From 6b8895b5a4b3449bd3c725882350a0a84dfa0516 Mon Sep 17 00:00:00 2001 From: Martez Reed Date: Tue, 24 Sep 2024 12:48:00 -0500 Subject: [PATCH] Add clouds datasource support (#284) --- CHANGELOG.md | 2 + docs/data-sources/cloud.md | 9 +- docs/data-sources/clouds.md | 43 ++++++ .../morpheus_clouds/data-source.tf | 7 + morpheus/data_source_cloud.go | 48 +++++++ morpheus/data_source_clouds.go | 127 ++++++++++++++++++ morpheus/provider.go | 1 + templates/data-sources/clouds.md.tmpl | 16 +++ 8 files changed, 252 insertions(+), 1 deletion(-) create mode 100644 docs/data-sources/clouds.md create mode 100644 examples/data-sources/morpheus_clouds/data-source.tf create mode 100644 morpheus/data_source_clouds.go create mode 100644 templates/data-sources/clouds.md.tmpl diff --git a/CHANGELOG.md b/CHANGELOG.md index ceeef9f..72adad7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,11 +2,13 @@ NOTES: +* Added support for the `morpheus_clouds` data source to lookup clouds and return a list of cloud ids. [233](https://github.com/gomorpheus/terraform-provider-morpheus/issues/233) * Update the `morpheus_chef_bootstrap_task` example to fix a typo referencing the server id attribute as `server_id` instead of `chef_server_id`. * Added support for the `morpheus_networks` data source to lookup networks and return a list of network ids. [280](https://github.com/gomorpheus/terraform-provider-morpheus/issues/280) FEATURES: +* **New Data Source:** `morpheus_clouds` * **New Data Source:** `morpheus_networks` ## 0.11.0 (September 10, 2024) diff --git a/docs/data-sources/cloud.md b/docs/data-sources/cloud.md index 5b21069..df7e336 100644 --- a/docs/data-sources/cloud.md +++ b/docs/data-sources/cloud.md @@ -27,5 +27,12 @@ data "morpheus_cloud" "vspherecloud" { ### Read-Only - `code` (String) Optional code for use with policies +- `costing_mode` (String) The costing mode of the cloud +- `external_id` (String) The external id of the cloud +- `group_ids` (Set of Number) The ids of the groups granted access to the cloud +- `guidance_mode` (String) The guidance mode of the cloud - `id` (Number) The ID of this resource. -- `location` (String) Optional location for your cloud \ No newline at end of file +- `inventory_level` (String) The inventory level of the cloud +- `labels` (Set of String) The organization labels associated with the cloud +- `location` (String) Optional location for your cloud +- `time_zone` (String) The time zone of the cloud \ No newline at end of file diff --git a/docs/data-sources/clouds.md b/docs/data-sources/clouds.md new file mode 100644 index 0000000..bfc0093 --- /dev/null +++ b/docs/data-sources/clouds.md @@ -0,0 +1,43 @@ +--- +page_title: "morpheus_clouds Data Source - terraform-provider-morpheus" +subcategory: "" +description: |- + Provides a Morpheus clouds data source. +--- + +# morpheus_clouds (Data Source) + +Provides a Morpheus clouds data source. + +## Example Usage + +```terraform +data "morpheus_clouds" "tf_example_clouds" { + sort_ascending = true + filter { + name = "name" + values = ["Test*"] + } +} +``` + + +## Schema + +### Optional + +- `filter` (Block Set) Custom filter block as described below. (see [below for nested schema](#nestedblock--filter)) +- `sort_ascending` (Boolean) Whether to sort the IDs in ascending order + +### Read-Only + +- `id` (String) The ID of this resource. +- `ids` (List of Number) + + +### Nested Schema for `filter` + +Required: + +- `name` (String) The name of the filter. Filter names are case-sensitive. Valid names are (name) +- `values` (Set of String) The filter values. Filter values are case-sensitive. Filters values support the use of Golang regex and can be tested at https://regex101.com/ \ No newline at end of file diff --git a/examples/data-sources/morpheus_clouds/data-source.tf b/examples/data-sources/morpheus_clouds/data-source.tf new file mode 100644 index 0000000..648265b --- /dev/null +++ b/examples/data-sources/morpheus_clouds/data-source.tf @@ -0,0 +1,7 @@ +data "morpheus_clouds" "tf_example_clouds" { + sort_ascending = true + filter { + name = "name" + values = ["Test*"] + } +} \ No newline at end of file diff --git a/morpheus/data_source_cloud.go b/morpheus/data_source_cloud.go index 2b5f1a6..f1eb9df 100644 --- a/morpheus/data_source_cloud.go +++ b/morpheus/data_source_cloud.go @@ -36,6 +36,43 @@ func dataSourceMorpheusCloud() *schema.Resource { Description: "Optional location for your cloud", Computed: true, }, + "external_id": { + Type: schema.TypeString, + Description: "The external id of the cloud", + Computed: true, + }, + "inventory_level": { + Type: schema.TypeString, + Description: "The inventory level of the cloud", + Computed: true, + }, + "guidance_mode": { + Type: schema.TypeString, + Description: "The guidance mode of the cloud", + Computed: true, + }, + "time_zone": { + Type: schema.TypeString, + Description: "The time zone of the cloud", + Computed: true, + }, + "costing_mode": { + Type: schema.TypeString, + Description: "The costing mode of the cloud", + Computed: true, + }, + "labels": { + Type: schema.TypeSet, + Description: "The organization labels associated with the cloud", + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "group_ids": { + Type: schema.TypeSet, + Description: "The ids of the groups granted access to the cloud", + Computed: true, + Elem: &schema.Schema{Type: schema.TypeInt}, + }, }, } } @@ -78,6 +115,17 @@ func dataSourceMorpheusCloudRead(ctx context.Context, d *schema.ResourceData, me d.Set("name", cloud.Name) d.Set("code", cloud.Code) d.Set("location", cloud.Location) + d.Set("external_id", cloud.ExternalID) + d.Set("inventory_level", cloud.InventoryLevel) + d.Set("guidance_mode", cloud.GuidanceMode) + d.Set("time_zone", cloud.TimeZone) + d.Set("costing_mode", cloud.CostingMode) + d.Set("labels", cloud.Labels) + var groupIds []int + for _, group := range cloud.Groups { + groupIds = append(groupIds, int(group.ID)) + } + d.Set("group_ids", groupIds) } else { return diag.Errorf("Cloud not found in response data.") // should not happen } diff --git a/morpheus/data_source_clouds.go b/morpheus/data_source_clouds.go new file mode 100644 index 0000000..78d5c03 --- /dev/null +++ b/morpheus/data_source_clouds.go @@ -0,0 +1,127 @@ +package morpheus + +import ( + "context" + "log" + + "github.com/gomorpheus/morpheus-go-sdk" + "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" +) + +func dataSourceMorpheusClouds() *schema.Resource { + return &schema.Resource{ + Description: "Provides a Morpheus clouds data source.", + ReadContext: dataSourceMorpheusCloudsRead, + Schema: map[string]*schema.Schema{ + "ids": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeInt}, + }, + "sort_ascending": { + Type: schema.TypeBool, + Description: "Whether to sort the IDs in ascending order", + Default: true, + Optional: true, + }, + "filter": { + Type: schema.TypeSet, + Description: "Custom filter block as described below.", + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Description: "The name of the filter. Filter names are case-sensitive. Valid names are (name)", + Required: true, + ValidateFunc: validation.StringInSlice([]string{"name"}, false), + }, + "values": { + Type: schema.TypeSet, + Description: "The filter values. Filter values are case-sensitive. Filters values support the use of Golang regex and can be tested at https://regex101.com/", + Required: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + }, + } +} + +func dataSourceMorpheusCloudsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + client := meta.(*morpheus.Client) + + // Warning or errors can be collected in a slice type + var diags diag.Diagnostics + + var resp *morpheus.Response + var err error + var sortOrder string + var names []string + + if len(d.Get("filter").(*schema.Set).List()) > 0 { + filters := d.Get("filter").(*schema.Set).List() + for _, filter := range filters { + filterPayload := filter.(map[string]interface{}) + + if filterPayload["name"].(string) == "name" { + for _, item := range filterPayload["values"].(*schema.Set).List() { + names = append(names, item.(string)) + } + } + } + } + + if len(names) == 0 { + names = append(names, "$") + } + + // Sort clouds in ascending or descending order + if d.Get("sort_ascending").(bool) { + sortOrder = "asc" + } else { + sortOrder = "desc" + } + + resp, err = client.ListClouds(&morpheus.Request{ + QueryParams: map[string]string{ + "max": "250", + "sort": "id", + "direction": sortOrder, + }, + }) + + if err != nil { + if resp != nil && resp.StatusCode == 404 { + log.Printf("API 404: %s - %v", resp, err) + return nil + } else { + log.Printf("API FAILURE: %s - %v", resp, err) + return diag.FromErr(err) + } + } + log.Printf("API RESPONSE: %s", resp) + + cloudIDs := []int64{} + + // store resource data + result := resp.Result.(*morpheus.ListCloudsResult) + clouds := result.Clouds + for _, cloud := range *clouds { + if len(names) > 0 { + if regexCheck(names, cloud.Name) { + cloudIDs = append(cloudIDs, cloud.ID) + } + } else { + cloudIDs = append(cloudIDs, cloud.ID) + } + } + d.SetId("1") + d.Set("ids", cloudIDs) + return diags +} diff --git a/morpheus/provider.go b/morpheus/provider.go index 60599b8..65c5456 100644 --- a/morpheus/provider.go +++ b/morpheus/provider.go @@ -187,6 +187,7 @@ func Provider() *schema.Provider { "morpheus_chef_server": dataSourceMorpheusChefServer(), "morpheus_cloud_datastore": dataSourceMorpheusCloudDatastore(), "morpheus_cloud": dataSourceMorpheusCloud(), + "morpheus_clouds": dataSourceMorpheusClouds(), "morpheus_cloud_type": dataSourceMorpheusCloudType(), "morpheus_cluster_type": dataSourceMorpheusClusterType(), "morpheus_contact": dataSourceMorpheusContact(), diff --git a/templates/data-sources/clouds.md.tmpl b/templates/data-sources/clouds.md.tmpl new file mode 100644 index 0000000..7835808 --- /dev/null +++ b/templates/data-sources/clouds.md.tmpl @@ -0,0 +1,16 @@ +--- +page_title: "morpheus_clouds Data Source - terraform-provider-morpheus" +subcategory: "" +description: |- +{{ .Description | plainmarkdown | trimspace | prefixlines " " }} +--- + +# morpheus_clouds (Data Source) + +{{ .Description | trimspace }} + +## Example Usage + +{{tffile "examples/data-sources/morpheus_clouds/data-source.tf"}} + +{{ .SchemaMarkdown | trimspace }} \ No newline at end of file