generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add device role and defaults for virtual machine which were lost on t…
…ransfering the code
- Loading branch information
Felix
committed
Aug 19, 2020
1 parent
90214b2
commit 9555af2
Showing
10 changed files
with
478 additions
and
22 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,57 @@ | ||
package netbox | ||
|
||
import ( | ||
"errors" | ||
"github.com/fbreckle/go-netbox/netbox/client" | ||
"github.com/fbreckle/go-netbox/netbox/client/dcim" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"strconv" | ||
) | ||
|
||
func dataSourceNetboxDeviceRole() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceNetboxDeviceRoleRead, | ||
Schema: map[string]*schema.Schema{ | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"slug": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"color_hex": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceNetboxDeviceRoleRead(d *schema.ResourceData, m interface{}) error { | ||
api := m.(*client.NetBox) | ||
|
||
name := d.Get("name").(string) | ||
params := dcim.NewDcimDeviceRolesListParams() | ||
params.Name = &name | ||
limit := int64(2) // Limit of 2 is enough | ||
params.Limit = &limit | ||
|
||
res, err := api.Dcim.DcimDeviceRolesList(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.SetId(strconv.FormatInt(result.ID, 10)) | ||
d.Set("name", result.Name) | ||
d.Set("slug", result.Slug) | ||
d.Set("color_hex", result.Color) | ||
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,33 @@ | ||
package netbox | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"testing" | ||
) | ||
|
||
func TestAccNetboxDeviceRoleDataSource_basic(t *testing.T) { | ||
|
||
testSlug := "dvrl_ds_basic" | ||
testName := testAccGetTestName(testSlug) | ||
resource.ParallelTest(t, resource.TestCase{ | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: fmt.Sprintf(` | ||
resource "netbox_device_role" "test" { | ||
name = "%[1]s" | ||
color_hex = "123456" | ||
} | ||
data "netbox_device_role" "test" { | ||
depends_on = [netbox_device_role.test] | ||
name = "%[1]s" | ||
}`, testName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrPair("data.netbox_device_role.test", "id", "netbox_device_role.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package netbox | ||
|
||
import ( | ||
"github.com/fbreckle/go-netbox/netbox/client" | ||
"github.com/fbreckle/go-netbox/netbox/client/dcim" | ||
"github.com/fbreckle/go-netbox/netbox/models" | ||
"github.com/go-openapi/runtime" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"strconv" | ||
) | ||
|
||
func resourceNetboxDeviceRole() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceNetboxDeviceRoleCreate, | ||
Read: resourceNetboxDeviceRoleRead, | ||
Update: resourceNetboxDeviceRoleUpdate, | ||
Delete: resourceNetboxDeviceRoleDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"slug": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"vm_role": &schema.Schema{ | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: true, | ||
}, | ||
"color_hex": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
}, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
} | ||
} | ||
|
||
func resourceNetboxDeviceRoleCreate(d *schema.ResourceData, m interface{}) error { | ||
api := m.(*client.NetBox) | ||
|
||
name := d.Get("name").(string) | ||
slugValue, slugOk := d.GetOk("slug") | ||
var slug string | ||
|
||
// Default slug to name if not given | ||
if !slugOk { | ||
slug = name | ||
} else { | ||
slug = slugValue.(string) | ||
} | ||
|
||
color := d.Get("color_hex").(string) | ||
vmRole := d.Get("vm_role").(bool) | ||
|
||
params := dcim.NewDcimDeviceRolesCreateParams().WithData( | ||
&models.DeviceRole{ | ||
Name: &name, | ||
Slug: &slug, | ||
Color: color, | ||
VMRole: vmRole, | ||
}, | ||
) | ||
|
||
res, err := api.Dcim.DcimDeviceRolesCreate(params, nil) | ||
if err != nil { | ||
//return errors.New(getTextFromError(err)) | ||
return err | ||
} | ||
|
||
d.SetId(strconv.FormatInt(res.GetPayload().ID, 10)) | ||
|
||
return resourceNetboxDeviceRoleRead(d, m) | ||
} | ||
|
||
func resourceNetboxDeviceRoleRead(d *schema.ResourceData, m interface{}) error { | ||
api := m.(*client.NetBox) | ||
id, _ := strconv.ParseInt(d.Id(), 10, 64) | ||
params := dcim.NewDcimDeviceRolesReadParams().WithID(id) | ||
|
||
res, err := api.Dcim.DcimDeviceRolesRead(params, nil) | ||
if err != nil { | ||
if apiError, ok := err.(*runtime.APIError); ok { | ||
errorcode := 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("vm_role", res.GetPayload().VMRole) | ||
d.Set("color_hex", res.GetPayload().Color) | ||
return nil | ||
} | ||
|
||
func resourceNetboxDeviceRoleUpdate(d *schema.ResourceData, m interface{}) error { | ||
api := m.(*client.NetBox) | ||
|
||
id, _ := strconv.ParseInt(d.Id(), 10, 64) | ||
data := models.DeviceRole{} | ||
|
||
name := d.Get("name").(string) | ||
color := d.Get("color_hex").(string) | ||
vmRole := d.Get("vm_role").(bool) | ||
|
||
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 | ||
data.Name = &name | ||
data.VMRole = vmRole | ||
data.Color = color | ||
|
||
params := dcim.NewDcimDeviceRolesPartialUpdateParams().WithID(id).WithData(&data) | ||
|
||
_, err := api.Dcim.DcimDeviceRolesPartialUpdate(params, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return resourceNetboxDeviceRoleRead(d, m) | ||
} | ||
|
||
func resourceNetboxDeviceRoleDelete(d *schema.ResourceData, m interface{}) error { | ||
api := m.(*client.NetBox) | ||
|
||
id, _ := strconv.ParseInt(d.Id(), 10, 64) | ||
params := dcim.NewDcimDeviceRolesDeleteParams().WithID(id) | ||
|
||
_, err := api.Dcim.DcimDeviceRolesDelete(params, nil) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
Oops, something went wrong.