Skip to content

Commit

Permalink
PLT-803: Support filter by status and health for appliances datasource.
Browse files Browse the repository at this point in the history
  • Loading branch information
nikchern committed Oct 27, 2023
1 parent d8cdd5a commit a6a70ee
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 25 deletions.
13 changes: 6 additions & 7 deletions docs/data-sources/appliance.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
page_title: "spectrocloud_appliance Data Source - terraform-provider-spectrocloud"
subcategory: ""
description: |-
Provides details about a single appliance.
---

# spectrocloud_appliance (Data Source)


Provides details about a single appliance.

## Example Usage

Expand All @@ -27,14 +27,13 @@ output "same" {

### Required

- `name` (String)

### Optional

- `tags` (Map of String)
- `name` (String) The name of the appliance.

### Read-Only

- `health` (String) The health of the appliance. Possible values are: 'healthy', 'unhealthy'.
- `id` (String) The ID of this resource.
- `status` (String) The status of the appliance. Possible values are: 'ready', 'in-use', 'unpaired'.
- `tags` (Map of String) The tags of the appliance.


5 changes: 5 additions & 0 deletions docs/data-sources/appliances.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ data "spectrocloud_appliances" "appliances" {
tags = {
"env" = "dev"
}
status = "in-use"
#status = "unpaired"
health = "healthy"
}
output "same" {
Expand All @@ -30,6 +33,8 @@ output "same" {

### Optional

- `health` (String) The health of the appliance. Possible values are: 'healthy', 'unhealthy'. If not specified, all appliances are returned.
- `status` (String) The status of the appliance. Possible values are: 'ready', 'in-use', 'unpaired'. If not specified, all appliances are returned.
- `tags` (Map of String) A list of tags to filter the appliances.

### Read-Only
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ provider GitHub [discussion board](https://github.com/spectrocloud/terraform-pro
- `host` (String) The Spectro Cloud API host url. Can also be set with the `SPECTROCLOUD_HOST` environment variable. Defaults to https://api.spectrocloud.com
- `ignore_insecure_tls_error` (Boolean) Ignore insecure TLS errors for Spectro Cloud API endpoints. Defaults to false.
- `password` (String, Sensitive, Deprecated) The Spectro Cloud user password. Can also be set with the `SPECTROCLOUD_PASSWORD` environment variable.
- `project_name` (String) The Spectro Cloud project name. If value is not provided or is an empty string it will be set to `Default`
- `project_name` (String) The Spectro Cloud project name. If value is not provided it will be set to `Default`
- `retry_attempts` (Number) Number of retry attempts. Can also be set with the `SPECTROCLOUD_RETRY_ATTEMPTS` environment variable. Defaults to 10.
- `trace` (Boolean) Enable HTTP request tracing. Can also be set with the `SPECTROCLOUD_TRACE` environment variable. To enable Terraform debug logging, set `TF_LOG=DEBUG`. Visit the Terraform documentation to learn more about Terraform [debugging](https://developer.hashicorp.com/terraform/plugin/log/managing).
- `username` (String, Deprecated) The Spectro Cloud username. Can also be set with the `SPECTROCLOUD_USERNAME` environment variable.
3 changes: 3 additions & 0 deletions examples/data-sources/spectrocloud_appliances/data-source.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ data "spectrocloud_appliances" "appliances" {
tags = {
"env" = "dev"
}
status = "in-use"
#status = "unpaired"
health = "healthy"
}

output "same" {
Expand Down
38 changes: 34 additions & 4 deletions spectrocloud/data_source_appliance.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,38 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

const APPLIANCE_STATUS_DESC = "The status of the appliance. Possible values are: 'ready', 'in-use', 'unpaired'. "
const APPLIANCE_HEALTH_DESC = "The health of the appliance. Possible values are: 'healthy', 'unhealthy'. "

func dataSourceAppliance() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceApplianceRead,

Description: "Provides details about a single appliance.",
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The name of the appliance. ",
},
"tags": {
Type: schema.TypeMap,
Optional: true,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "The tags of the appliance.",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: APPLIANCE_STATUS_DESC,
},
"health": {
Type: schema.TypeString,
Computed: true,
Description: APPLIANCE_HEALTH_DESC,
},
},
}
Expand All @@ -47,6 +63,20 @@ func dataSourceApplianceRead(_ context.Context, d *schema.ResourceData, m interf
if err != nil {
return diag.FromErr(err)
}

if appliance.Status != nil {
err = d.Set("status", appliance.Status.State)
if err != nil {
return diag.FromErr(err)
}
}

if appliance.Status != nil && appliance.Status.Health != nil {
err = d.Set("health", appliance.Status.Health.State)
if err != nil {
return diag.FromErr(err)
}
}
}
return diags
}
24 changes: 23 additions & 1 deletion spectrocloud/data_source_appliances.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ func dataSourceAppliances() *schema.Resource {
Type: schema.TypeString,
},
},
"status": {
Type: schema.TypeString,
Description: APPLIANCE_STATUS_DESC + " If not specified, all appliances are returned.",
Optional: true,
},
"health": {
Type: schema.TypeString,
Description: APPLIANCE_HEALTH_DESC + " If not specified, all appliances are returned.",
Optional: true,
},
},
}
}
Expand All @@ -50,7 +60,19 @@ func dataSourcesApplianceRead(_ context.Context, d *schema.ResourceData, m inter

// prepare filter
check := func(edgeHostDevice *models.V1EdgeHostDevice) bool {
return IsMapSubset(edgeHostDevice.Metadata.Labels, tags)
is := false
is = is || IsMapSubset(edgeHostDevice.Metadata.Labels, tags)
if d.Get("status") != nil && d.Get("status").(string) != "" && edgeHostDevice.Status != nil {
is = is && edgeHostDevice.Status.State == d.Get("status").(string)
}
if d.Get("health") != nil && d.Get("health").(string) != "" && edgeHostDevice.Status != nil {
if edgeHostDevice.Status.Health == nil || edgeHostDevice.Status.Health.State == "" {
return false // if health is not set, it's not a match
} else {
is = is && edgeHostDevice.Status.Health.State == d.Get("health").(string)
}
}
return is
}

// apply filter
Expand Down
1 change: 0 additions & 1 deletion spectrocloud/data_source_cluster_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func dataSourceClusterProfile() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Default: "project",
Computed: true,
ValidateFunc: validation.StringInSlice([]string{"", "project", "tenant", "system"}, false),
Description: "Cluster profile context can be `project` or `tenant`. " +
"Defaults to `project`." + PROJECT_NAME_NUANCE,
Expand Down
23 changes: 12 additions & 11 deletions spectrocloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

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

"github.com/spectrocloud/palette-sdk-go/client"
)
Expand Down Expand Up @@ -62,10 +63,12 @@ func New(_ string) func() *schema.Provider {
DefaultFunc: schema.EnvDefaultFunc("SPECTROCLOUD_RETRY_ATTEMPTS", 10),
},
"project_name": {
Type: schema.TypeString,
Optional: true,
Default: "Default",
Description: "The Spectro Cloud project name. If value is not provided or is an empty string it will be set to `Default`",
Type: schema.TypeString,
Optional: true,
Default: "Default",
// cannot be empty
ValidateFunc: validation.StringIsNotEmpty,
Description: "The Spectro Cloud project name. If value is not provided it will be set to `Default`",
},
"ignore_insecure_tls_error": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -237,15 +240,13 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}

c := client.New(host, username, password, "", apiKey, transportDebug, retryAttempts)

if projectName != "" {
uid, err := c.GetProjectUID(projectName)
if err != nil {
return nil, diag.FromErr(err)
}

c = client.New(host, username, password, uid, apiKey, transportDebug, retryAttempts)
uid, err := c.GetProjectUID(projectName)
if err != nil {
return nil, diag.FromErr(err)
}

c = client.New(host, username, password, uid, apiKey, transportDebug, retryAttempts)

return c, diags

}

0 comments on commit a6a70ee

Please sign in to comment.