-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add clouds datasource support (#284)
- Loading branch information
Showing
8 changed files
with
252 additions
and
1 deletion.
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
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,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/ |
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,7 @@ | ||
data "morpheus_clouds" "tf_example_clouds" { | ||
sort_ascending = true | ||
filter { | ||
name = "name" | ||
values = ["Test*"] | ||
} | ||
} |
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,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 | ||
} |
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,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 }} |