Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions examples/resources/rafay_namespace/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,29 @@ resource "rafay_namespace" "tfdemonamespacewithmesh" {
}
}
}

#Example of data namespace
data "rafay_namespace" "tfdemonamespace1" {
metadata {
name = "tfdemonamespace1"
project = "terraform"
}
}

output "tfdemonamespace_out" {
description = "spec"
value = data.rafay_namespace.tfdemonamespace1.spec
}

#Example of data namespaces
data "rafay_namespaces" "all" {
metadata {
project = "terraform"
}
}

output "allnamespaces" {
description = "spec"
value = data.rafay_namespaces.all
}

78 changes: 78 additions & 0 deletions rafay/data_namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package rafay

import (
"context"
"fmt"
"log"
"os"
"strings"
"time"

"github.com/RafaySystems/rafay-common/pkg/hub/client/options"
typed "github.com/RafaySystems/rafay-common/pkg/hub/client/typed"
"github.com/RafaySystems/rafay-common/pkg/hub/terraform/resource"
"github.com/RafaySystems/rctl/pkg/config"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataNamespace() *schema.Resource {
return &schema.Resource{
Description: "The Namespace data source allows access to the Rafay Namespace resource",
ReadContext: dataNamespaceRead,
Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(10 * time.Minute),
},
SchemaVersion: 1,
Schema: resource.NamespaceSchema.Schema,
}
}

func dataNamespaceRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics

log.Println("dataNamespaceRead ")
tflog := os.Getenv("TF_LOG")
if tflog == "TRACE" || tflog == "DEBUG" {
ctx = context.WithValue(ctx, "debug", "true")
}

meta := GetMetaData(d)
if meta == nil {
return diag.FromErr(fmt.Errorf("%s", "failed to read resource "))
}
if d.State() != nil && d.State().ID != "" {
meta.Name = d.State().ID
}

auth := config.GetConfig().GetAppAuthProfile()
client, err := typed.NewClientWithUserAgent(auth.URL, auth.Key, TF_USER_AGENT, options.WithInsecureSkipVerify(auth.SkipServerCertValid))
if err != nil {
return diag.FromErr(err)
}

Namespace, err := client.InfraV3().Namespace().Get(ctx, options.GetOptions{
Name: meta.Name,
Project: meta.Project,
})
if err != nil {
if strings.Contains(err.Error(), "code 404") {
log.Println("Resource Read ", "error", err)
d.SetId("")
return diags
}
return diag.FromErr(err)
}

err = flattenNamespace(d, Namespace)
if err != nil {
return diag.FromErr(err)
}

d.SetId(Namespace.Metadata.Name)

return diags

}
92 changes: 92 additions & 0 deletions rafay/data_namespaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package rafay

import (
"context"
"fmt"
"log"
"os"

"github.com/RafaySystems/rafay-common/pkg/hub/client/options"
typed "github.com/RafaySystems/rafay-common/pkg/hub/client/typed"
"github.com/RafaySystems/rctl/pkg/config"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataNamespaces() *schema.Resource {
return &schema.Resource{
ReadContext: dataNamespacesRead,
Schema: map[string]*schema.Schema{
"metadata": &schema.Schema{
Description: "Metadata of the namespace resource",
Elem: &schema.Resource{Schema: map[string]*schema.Schema{
"project": &schema.Schema{
Description: "Project of the resource",
Optional: true,
Type: schema.TypeString,
},
}},
MaxItems: 1,
MinItems: 1,
Optional: true,
Type: schema.TypeList,
},
"namespaces": &schema.Schema{
Description: "Specification of the namespace resource",
Elem: &schema.Resource{Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Description: "data is the base64 encoded contents of the file",
Optional: true,
Type: schema.TypeString,
},
}},
Optional: true,
Type: schema.TypeList,
},
},
}
}

func dataNamespacesRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics

log.Println("dataNamespacesRead ")
tflog := os.Getenv("TF_LOG")
if tflog == "TRACE" || tflog == "DEBUG" {
ctx = context.WithValue(ctx, "debug", "true")
}

meta := GetMetaData(d)
if meta == nil {
return diag.FromErr(fmt.Errorf("%s", "failed to read resource "))
}

auth := config.GetConfig().GetAppAuthProfile()
client, err := typed.NewClientWithUserAgent(auth.URL, auth.Key, TF_USER_AGENT, options.WithInsecureSkipVerify(auth.SkipServerCertValid))
if err != nil {
return diag.FromErr(err)
}

namespaces, err := client.InfraV3().Namespace().List(ctx, options.ListOptions{
Project: meta.Project,
})
if err != nil {
return diag.FromErr(err)
}

var namespaceList []map[string]interface{}

for _, ns := range namespaces.Items {
nsData := map[string]interface{}{
"name": ns.Metadata.Name,
}
namespaceList = append(namespaceList, nsData)
}

d.Set("namespaces", namespaceList)
d.SetId("All")
return diags

}
2 changes: 2 additions & 0 deletions rafay/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ func New(_ string) func() *schema.Provider {
"rafay_groupassociation": dataGroupAssociation(),
"rafay_workload": dataWorkload(),
"rafay_cluster_blueprint_status": dataClusterBlueprintStatus(),
"rafay_namespace": dataNamespace(),
"rafay_namespaces": dataNamespaces(),
},
ConfigureContextFunc: providerConfigure,
}
Expand Down