diff --git a/docs/resources/namespace.md b/docs/resources/namespace.md index d6b828a3..4f4ac4f1 100644 --- a/docs/resources/namespace.md +++ b/docs/resources/namespace.md @@ -357,3 +357,31 @@ Optional: --- - `id` - (String) The ID of the resource, generated by the system after you create the resource. + +# rafay_namespace (data source) +## Example Usage + +--- + +```terraform +data "rafay_namespace" "tfdemonamespace1" { + metadata { + name = "tfdemonamespace1" + project = "terraform" + } +} + +output "addon_spec" { + description = "spec" + value = data.rafay_namespace.tfdemonamespace1.spec +} +``` +--- + +## Argument Reference + +--- +***Required*** +- `name` - Contains name of the namespace +- `project` - Contains the name of the project +--- diff --git a/docs/resources/namespaces.md b/docs/resources/namespaces.md new file mode 100644 index 00000000..ed2a5793 --- /dev/null +++ b/docs/resources/namespaces.md @@ -0,0 +1,41 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "rafay_namespaces Resource - terraform-provider-rafay" +subcategory: "" +description: |- + +--- + +# rafay_namespaces (Data Source) + +Namespaces help all namespaces in a given project. + +## Example Usage + +--- + +The following is a simple example that demonstrates how to get all namespaces. + +```terraform +#Basic example for namespace +data "rafay_namespaces" "all" { + metadata { + project = "terraform" + } +} + +output "allnamespaces" { + description = "spec" + value = data.rafay_namespaces.all +} +``` + +--- + +## Argument Reference + +--- +***Required*** +- `name` - Contains name of the namespace +- `project` - Contains the name of the project +--- diff --git a/examples/resources/rafay_namespace/data.tf b/examples/resources/rafay_namespace/data.tf new file mode 100644 index 00000000..42269584 --- /dev/null +++ b/examples/resources/rafay_namespace/data.tf @@ -0,0 +1,24 @@ +#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 +} diff --git a/examples/resources/rafay_namespace/resource.tf b/examples/resources/rafay_namespace/resource.tf index 38636703..9990e447 100644 --- a/examples/resources/rafay_namespace/resource.tf +++ b/examples/resources/rafay_namespace/resource.tf @@ -124,3 +124,4 @@ resource "rafay_namespace" "tfdemonamespacewithmesh" { } } } + diff --git a/rafay/data_namespace.go b/rafay/data_namespace.go new file mode 100644 index 00000000..a5ff4a7d --- /dev/null +++ b/rafay/data_namespace.go @@ -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 + +} diff --git a/rafay/data_namespaces.go b/rafay/data_namespaces.go new file mode 100644 index 00000000..0e3df40d --- /dev/null +++ b/rafay/data_namespaces.go @@ -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 + +} diff --git a/rafay/provider.go b/rafay/provider.go index a3c4ed52..fddcb327 100644 --- a/rafay/provider.go +++ b/rafay/provider.go @@ -114,6 +114,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, }