Skip to content

Commit

Permalink
NSOF-5982 metaport_cluster: support data-source name param
Browse files Browse the repository at this point in the history
Even though name is not unique for metaport clusters - costumer's feedback
brought the need to onboard metaports and metaport clusters outside of tf code and later - use
the metaport name to find it and attach mapped elements to it from
terraform.

Because it's not unique - in case there is more then one metaport cluster with the
specified name - an error will be returned.

Customer's ticket: #11
  • Loading branch information
hod-alpert committed Feb 6, 2022
1 parent d0e12af commit 9d5a2e8
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 15 deletions.
18 changes: 13 additions & 5 deletions docs/data-sources/metaport_cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 generated by tfplugindocs -->
## 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)
14 changes: 11 additions & 3 deletions examples/data-sources/pfptmeta_metaport_cluster/data-source.tf
Original file line number Diff line number Diff line change
@@ -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
}
32 changes: 32 additions & 0 deletions internal/client/metaport_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 28 additions & 2 deletions internal/provider/acc_tests/metaport_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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-.+$"),
Expand Down Expand Up @@ -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"
}`
10 changes: 8 additions & 2 deletions internal/provider/metaport_cluster/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 5 additions & 3 deletions internal/provider/metaport_cluster/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 9d5a2e8

Please sign in to comment.