Skip to content

Commit

Permalink
Add clouds datasource support (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
martezr authored Sep 24, 2024
1 parent 9ec537e commit 6b8895b
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion docs/data-sources/cloud.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
- `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
43 changes: 43 additions & 0 deletions docs/data-sources/clouds.md
Original file line number Diff line number Diff line change
@@ -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 generated by tfplugindocs -->
## 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)

<a id="nestedblock--filter"></a>
### 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/
7 changes: 7 additions & 0 deletions examples/data-sources/morpheus_clouds/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
data "morpheus_clouds" "tf_example_clouds" {
sort_ascending = true
filter {
name = "name"
values = ["Test*"]
}
}
48 changes: 48 additions & 0 deletions morpheus/data_source_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
},
},
}
}
Expand Down Expand Up @@ -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
}
Expand Down
127 changes: 127 additions & 0 deletions morpheus/data_source_clouds.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions morpheus/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
16 changes: 16 additions & 0 deletions templates/data-sources/clouds.md.tmpl
Original file line number Diff line number Diff line change
@@ -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 }}

0 comments on commit 6b8895b

Please sign in to comment.