generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from e-breuninger/feature/cluster_groups
Add cluster group resource Closes #10
- Loading branch information
Showing
9 changed files
with
376 additions
and
9 deletions.
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,52 @@ | ||
package netbox | ||
|
||
import ( | ||
"errors" | ||
"github.com/fbreckle/go-netbox/netbox/client" | ||
"github.com/fbreckle/go-netbox/netbox/client/virtualization" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"strconv" | ||
) | ||
|
||
func dataSourceNetboxClusterGroup() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceNetboxClusterGroupRead, | ||
Schema: map[string]*schema.Schema{ | ||
"cluster_group_id": &schema.Schema{ | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceNetboxClusterGroupRead(d *schema.ResourceData, m interface{}) error { | ||
api := m.(*client.NetBoxAPI) | ||
|
||
name := d.Get("name").(string) | ||
params := virtualization.NewVirtualizationClusterGroupsListParams() | ||
params.Name = &name | ||
limit := int64(2) // Limit of 2 is enough | ||
params.Limit = &limit | ||
|
||
res, err := api.Virtualization.VirtualizationClusterGroupsList(params, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if *res.GetPayload().Count > int64(1) { | ||
return errors.New("More than one result. Specify a more narrow filter") | ||
} | ||
if *res.GetPayload().Count == int64(0) { | ||
return errors.New("No result") | ||
} | ||
result := res.GetPayload().Results[0] | ||
d.Set("cluster_group_id", result.ID) | ||
d.SetId(strconv.FormatInt(result.ID, 10)) | ||
d.Set("name", result.Name) | ||
return nil | ||
} |
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,34 @@ | ||
package netbox | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"testing" | ||
) | ||
|
||
func TestAccNetboxClusterGroupDataSource_basic(t *testing.T) { | ||
|
||
testSlug := "clstrgrp_ds_basic" | ||
testName := testAccGetTestName(testSlug) | ||
resource.ParallelTest(t, resource.TestCase{ | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: fmt.Sprintf(` | ||
resource "netbox_cluster_group" "test" { | ||
name = "%[1]s" | ||
} | ||
data "netbox_cluster_group" "test" { | ||
depends_on = [netbox_cluster_group.test] | ||
name = "%[1]s" | ||
}`, testName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrPair("data.netbox_cluster_group.test", "cluster_group_id", "netbox_cluster_group.test", "id"), | ||
resource.TestCheckResourceAttrPair("data.netbox_cluster_group.test", "id", "netbox_cluster_group.test", "id"), | ||
), | ||
ExpectNonEmptyPlan: true, | ||
}, | ||
}, | ||
}) | ||
} |
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,147 @@ | ||
package netbox | ||
|
||
import ( | ||
"github.com/fbreckle/go-netbox/netbox/client" | ||
"github.com/fbreckle/go-netbox/netbox/client/virtualization" | ||
"github.com/fbreckle/go-netbox/netbox/models" | ||
"github.com/go-openapi/runtime" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
"strconv" | ||
) | ||
|
||
func resourceNetboxClusterGroup() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceNetboxClusterGroupCreate, | ||
Read: resourceNetboxClusterGroupRead, | ||
Update: resourceNetboxClusterGroupUpdate, | ||
Delete: resourceNetboxClusterGroupDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"slug": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ValidateFunc: validation.StringLenBetween(0, 30), | ||
}, | ||
"description": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
}, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
} | ||
} | ||
|
||
func resourceNetboxClusterGroupCreate(d *schema.ResourceData, m interface{}) error { | ||
api := m.(*client.NetBoxAPI) | ||
|
||
data := models.ClusterGroup{} | ||
|
||
name := d.Get("name").(string) | ||
data.Name = &name | ||
|
||
slugValue, slugOk := d.GetOk("slug") | ||
var slug string | ||
// Default slug to name attribute if not given | ||
if !slugOk { | ||
slug = name | ||
} else { | ||
slug = slugValue.(string) | ||
} | ||
data.Slug = &slug | ||
|
||
if description, ok := d.GetOk("description"); ok { | ||
data.Description = description.(string) | ||
} | ||
|
||
params := virtualization.NewVirtualizationClusterGroupsCreateParams().WithData(&data) | ||
|
||
res, err := api.Virtualization.VirtualizationClusterGroupsCreate(params, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(strconv.FormatInt(res.GetPayload().ID, 10)) | ||
|
||
return resourceNetboxClusterGroupRead(d, m) | ||
} | ||
|
||
func resourceNetboxClusterGroupRead(d *schema.ResourceData, m interface{}) error { | ||
api := m.(*client.NetBoxAPI) | ||
id, _ := strconv.ParseInt(d.Id(), 10, 64) | ||
params := virtualization.NewVirtualizationClusterGroupsReadParams().WithID(id) | ||
|
||
res, err := api.Virtualization.VirtualizationClusterGroupsRead(params, nil) | ||
if err != nil { | ||
errorcode := err.(*runtime.APIError).Response.(runtime.ClientResponse).Code() | ||
if errorcode == 404 { | ||
// If the ID is updated to blank, this tells Terraform the resource no longer exists (maybe it was destroyed out of band). Just like the destroy callback, the Read function should gracefully handle this case. https://www.terraform.io/docs/extend/writing-custom-providers.html | ||
d.SetId("") | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
d.Set("name", res.GetPayload().Name) | ||
d.Set("slug", res.GetPayload().Slug) | ||
d.Set("description", res.GetPayload().Description) | ||
return nil | ||
} | ||
|
||
func resourceNetboxClusterGroupUpdate(d *schema.ResourceData, m interface{}) error { | ||
api := m.(*client.NetBoxAPI) | ||
|
||
id, _ := strconv.ParseInt(d.Id(), 10, 64) | ||
data := models.ClusterGroup{} | ||
|
||
name := d.Get("name").(string) | ||
data.Name = &name | ||
|
||
slugValue, slugOk := d.GetOk("slug") | ||
var slug string | ||
// Default slug to name if not given | ||
if !slugOk { | ||
slug = name | ||
} else { | ||
slug = slugValue.(string) | ||
} | ||
data.Slug = &slug | ||
|
||
if d.HasChange("description") { | ||
// description omits empty values so set to ' ' | ||
if description := d.Get("description"); description.(string) == "" { | ||
data.Description = " " | ||
} else { | ||
data.Description = description.(string) | ||
} | ||
} | ||
|
||
params := virtualization.NewVirtualizationClusterGroupsPartialUpdateParams().WithID(id).WithData(&data) | ||
|
||
_, err := api.Virtualization.VirtualizationClusterGroupsPartialUpdate(params, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return resourceNetboxClusterGroupRead(d, m) | ||
} | ||
|
||
func resourceNetboxClusterGroupDelete(d *schema.ResourceData, m interface{}) error { | ||
api := m.(*client.NetBoxAPI) | ||
|
||
id, _ := strconv.ParseInt(d.Id(), 10, 64) | ||
params := virtualization.NewVirtualizationClusterGroupsDeleteParams().WithID(id) | ||
|
||
_, err := api.Virtualization.VirtualizationClusterGroupsDelete(params, nil) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
Oops, something went wrong.