Skip to content

Commit

Permalink
Merge pull request #711 from RafaySystems/RC-37900-Support-for-the-da…
Browse files Browse the repository at this point in the history
…ta-source-for-the-rafay_namespace-resource-v2-10

RC-37900-Support-for-the-data-source-for-the-rafay_namespace-resource-v2-10
  • Loading branch information
gumpumadhu authored Oct 16, 2024
2 parents d17b5d4 + b078903 commit 48ce7c4
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 0 deletions.
28 changes: 28 additions & 0 deletions docs/resources/namespace.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
41 changes: 41 additions & 0 deletions docs/resources/namespaces.md
Original file line number Diff line number Diff line change
@@ -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
---
24 changes: 24 additions & 0 deletions examples/resources/rafay_namespace/data.tf
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions examples/resources/rafay_namespace/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,4 @@ resource "rafay_namespace" "tfdemonamespacewithmesh" {
}
}
}

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 @@ -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,
}
Expand Down

0 comments on commit 48ce7c4

Please sign in to comment.