-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #711 from RafaySystems/RC-37900-Support-for-the-da…
…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
Showing
7 changed files
with
266 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,3 +124,4 @@ resource "rafay_namespace" "tfdemonamespacewithmesh" { | |
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters