diff --git a/docs/data-sources/metaport_cluster.md b/docs/data-sources/metaport_cluster.md index cddbd16e..bc18904f 100644 --- a/docs/data-sources/metaport_cluster.md +++ b/docs/data-sources/metaport_cluster.md @@ -13,25 +13,33 @@ MetaPort cluster defines a group of highly-available MetaPorts that are deployed ## Example Usage ```terraform -data "pfptmeta_metaport_cluster" "metaport_cluster" { +data "pfptmeta_metaport_cluster" "metaport_cluster_by_id" { id = "mpc-123" } -output "metaport_cluster" { - value = data.pfptmeta_metaport_cluster.metaport_cluster +output "metaport_cluster_by_id" { + value = data.pfptmeta_metaport_cluster.metaport_cluster_by_id +} + +data "pfptmeta_metaport_cluster" "metaport_cluster_by_name" { + name = "metaport cluster name" +} + +output "metaport_cluster_by_name" { + value = data.pfptmeta_metaport_cluster.metaport_cluster_by_name } ``` ## Schema -### Required +### Optional - **id** (String) The ID of this resource. +- **name** (String) ### Read-Only - **description** (String) - **mapped_elements** (Set of String) List of mapped element IDs - **metaports** (Set of String) List of MetaPort IDs -- **name** (String) diff --git a/examples/data-sources/pfptmeta_metaport_cluster/data-source.tf b/examples/data-sources/pfptmeta_metaport_cluster/data-source.tf index 919cd589..deb8f55a 100644 --- a/examples/data-sources/pfptmeta_metaport_cluster/data-source.tf +++ b/examples/data-sources/pfptmeta_metaport_cluster/data-source.tf @@ -1,7 +1,15 @@ -data "pfptmeta_metaport_cluster" "metaport_cluster" { +data "pfptmeta_metaport_cluster" "metaport_cluster_by_id" { id = "mpc-123" } -output "metaport_cluster" { - value = data.pfptmeta_metaport_cluster.metaport_cluster +output "metaport_cluster_by_id" { + value = data.pfptmeta_metaport_cluster.metaport_cluster_by_id +} + +data "pfptmeta_metaport_cluster" "metaport_cluster_by_name" { + name = "metaport cluster name" +} + +output "metaport_cluster_by_name" { + value = data.pfptmeta_metaport_cluster.metaport_cluster_by_name } \ No newline at end of file diff --git a/internal/client/metaport_cluster.go b/internal/client/metaport_cluster.go index ee7ecab7..346142ab 100644 --- a/internal/client/metaport_cluster.go +++ b/internal/client/metaport_cluster.go @@ -72,6 +72,38 @@ func GetMetaportCluster(ctx context.Context, c *Client, mId string) (*MetaportCl return parseMetaportCluster(resp) } +func GetMetaportClustertByName(ctx context.Context, c *Client, name string) (*MetaportCluster, error) { + url := fmt.Sprintf("%s/%s", c.BaseURL, metaportClusterEndpoint) + resp, err := c.Get(ctx, url, u.Values{"expand": {"true"}}) + if err != nil { + return nil, err + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("could not read metaport cluster response") + } + var respBody []MetaportCluster + err = json.Unmarshal(body, &respBody) + if err != nil { + return nil, fmt.Errorf("could not parse metaport cluster response: %v", err) + } + var nameMatch []MetaportCluster + for _, m := range respBody { + if m.Name == name { + nameMatch = append(nameMatch, m) + } + } + switch len(nameMatch) { + case 0: + return nil, fmt.Errorf("could not find metaport cluster with name \"%s\"", name) + case 1: + return &nameMatch[0], nil + default: + return nil, fmt.Errorf("found more then one metaport cluster with name \"%s\"", name) + } +} + func UpdateMetaportCluster(ctx context.Context, c *Client, mId string, m *MetaportCluster) (*MetaportCluster, error) { neUrl := fmt.Sprintf("%s/%s/%s", c.BaseURL, metaportClusterEndpoint, mId) body, err := json.Marshal(m) diff --git a/internal/provider/acc_tests/metaport_cluster_test.go b/internal/provider/acc_tests/metaport_cluster_test.go index 6d3bb0f0..6a71a062 100644 --- a/internal/provider/acc_tests/metaport_cluster_test.go +++ b/internal/provider/acc_tests/metaport_cluster_test.go @@ -57,7 +57,27 @@ func TestAccDataMetaportCluster(t *testing.T) { CheckDestroy: validateResourceDestroyed("metaport_cluster", "v1/metaport_clusters"), Steps: []resource.TestStep{ { - Config: testAccMetaportClusterStep1 + testAccDataSourceMetaportCluster, + Config: testAccMetaportClusterStep1 + testAccDataSourceMetaportClusterByID, + Check: resource.ComposeTestCheckFunc( + resource.TestMatchResourceAttr( + "data.pfptmeta_metaport_cluster.metaport_cluster", "id", regexp.MustCompile("^mpc-.+$"), + ), + resource.TestCheckResourceAttr( + "data.pfptmeta_metaport_cluster.metaport_cluster", "name", "metaport cluster name", + ), + resource.TestCheckResourceAttr( + "data.pfptmeta_metaport_cluster.metaport_cluster", "description", "metaport cluster description", + ), + resource.TestMatchResourceAttr( + "data.pfptmeta_metaport_cluster.metaport_cluster", "mapped_elements.0", regexp.MustCompile("^ne-[\\d]+$"), + ), + resource.TestMatchResourceAttr( + "data.pfptmeta_metaport_cluster.metaport_cluster", "metaports.0", regexp.MustCompile("^mp-[\\d]+$"), + ), + ), + }, + { + Config: testAccMetaportClusterStep1 + testAccDataSourceMetaportClusterByName, Check: resource.ComposeTestCheckFunc( resource.TestMatchResourceAttr( "data.pfptmeta_metaport_cluster.metaport_cluster", "id", regexp.MustCompile("^mpc-.+$"), @@ -116,8 +136,14 @@ resource "pfptmeta_metaport_cluster" "metaport_cluster" { } ` -const testAccDataSourceMetaportCluster = ` +const testAccDataSourceMetaportClusterByID = ` data "pfptmeta_metaport_cluster" "metaport_cluster" { id = pfptmeta_metaport_cluster.metaport_cluster.id }` + +const testAccDataSourceMetaportClusterByName = ` + +data "pfptmeta_metaport_cluster" "metaport_cluster" { + name = "metaport cluster name" +}` diff --git a/internal/provider/metaport_cluster/common.go b/internal/provider/metaport_cluster/common.go index 01545c03..309c6861 100644 --- a/internal/provider/metaport_cluster/common.go +++ b/internal/provider/metaport_cluster/common.go @@ -18,9 +18,15 @@ var excludedKeys = []string{"id"} func metaportClusterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - id := d.Get("id").(string) c := meta.(*client.Client) - m, err := client.GetMetaportCluster(ctx, c, id) + var err error + var m *client.MetaportCluster + if id, exists := d.GetOk("id"); exists { + m, err = client.GetMetaportCluster(ctx, c, id.(string)) + } + if name, exists := d.GetOk("name"); exists { + m, err = client.GetMetaportClustertByName(ctx, c, name.(string)) + } if err != nil { errResponse, ok := err.(*client.ErrorResponse) if ok && errResponse.Status == http.StatusNotFound { diff --git a/internal/provider/metaport_cluster/data_source.go b/internal/provider/metaport_cluster/data_source.go index 2072a1f6..0e53e372 100644 --- a/internal/provider/metaport_cluster/data_source.go +++ b/internal/provider/metaport_cluster/data_source.go @@ -14,12 +14,14 @@ func DataSource() *schema.Resource { Schema: map[string]*schema.Schema{ "id": { Type: schema.TypeString, - Required: true, + ExactlyOneOf: []string{"name"}, + Optional: true, ValidateDiagFunc: common.ValidateID(false, "mpc"), }, "name": { - Type: schema.TypeString, - Computed: true, + ExactlyOneOf: []string{"id"}, + Type: schema.TypeString, + Optional: true, }, "description": { Type: schema.TypeString,