Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for physical and shipping address in sites #337

Merged
merged 1 commit into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions netbox/resource_netbox_site.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ func resourceNetboxSite() *schema.Resource {
Type: schema.TypeFloat,
Optional: true,
},
"physical_address": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(0, 200),
},
"shipping_address": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(0, 200),
},
"region_id": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -126,6 +136,16 @@ func resourceNetboxSiteCreate(d *schema.ResourceData, m interface{}) error {
data.Longitude = float64ToPtr(float64(longitudeValue.(float64)))
}

physicalAddressValue, ok := d.GetOk("physical_address")
if ok {
data.PhysicalAddress = physicalAddressValue.(string)
}

shippingAddressValue, ok := d.GetOk("shipping_address")
if ok {
data.ShippingAddress = shippingAddressValue.(string)
}

regionIDValue, ok := d.GetOk("region_id")
if ok {
data.Region = int64ToPtr(int64(regionIDValue.(int)))
Expand Down Expand Up @@ -195,6 +215,8 @@ func resourceNetboxSiteRead(d *schema.ResourceData, m interface{}) error {
d.Set("facility", site.Facility)
d.Set("longitude", site.Longitude)
d.Set("latitude", site.Latitude)
d.Set("physical_address", site.PhysicalAddress)
d.Set("shipping_address", site.ShippingAddress)
d.Set("timezone", site.TimeZone)
d.Set("asn_ids", getIDsFromNestedASNList(site.Asns))

Expand Down Expand Up @@ -265,6 +287,22 @@ func resourceNetboxSiteUpdate(d *schema.ResourceData, m interface{}) error {
data.Longitude = float64ToPtr(float64(longitudeValue.(float64)))
}

physicalAddressValue, ok := d.GetOk("physical_address")
if ok {
data.PhysicalAddress = physicalAddressValue.(string)
} else if d.HasChange("physical_address") {
// If GetOK returned unset description and its value changed, set it as a space string to delete it ...
data.PhysicalAddress = " "
}

shippingAddressValue, ok := d.GetOk("shipping_address")
if ok {
data.ShippingAddress = shippingAddressValue.(string)
} else if d.HasChange("shipping_address") {
// If GetOK returned unset description and its value changed, set it as a space string to delete it ...
data.ShippingAddress = " "
}

regionIDValue, ok := d.GetOk("region_id")
if ok {
data.Region = int64ToPtr(int64(regionIDValue.(int)))
Expand Down
11 changes: 11 additions & 0 deletions netbox/resource_netbox_site_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ resource "netbox_site" "test" {
status = "planned"
description = "%[1]s"
facility = "%[1]s"
physical_address = "%[1]s"
shipping_address = "%[1]s"
asn_ids = [netbox_asn.test.id]
group_id = netbox_site_group.test.id
}`, testName, randomSlug),
Expand All @@ -49,6 +51,8 @@ resource "netbox_site" "test" {
resource.TestCheckResourceAttr("netbox_site.test", "status", "planned"),
resource.TestCheckResourceAttr("netbox_site.test", "description", testName),
resource.TestCheckResourceAttr("netbox_site.test", "facility", testName),
resource.TestCheckResourceAttr("netbox_site.test", "physical_address", testName),
resource.TestCheckResourceAttr("netbox_site.test", "shipping_address", testName),
resource.TestCheckResourceAttr("netbox_site.test", "asn_ids.#", "1"),
resource.TestCheckResourceAttrPair("netbox_site.test", "asn_ids.0", "netbox_asn.test", "id"),
resource.TestCheckResourceAttrPair("netbox_site.test", "group_id", "netbox_site_group.test", "id"),
Expand Down Expand Up @@ -145,9 +149,14 @@ func TestAccNetboxSite_fieldUpdate(t *testing.T) {
resource "netbox_site" "test" {
name = "%[2]s"
description = "Test site description"
physical_address = "Physical address"
shipping_address = "Shipping address"

}`, testField, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_site.test", "description", "Test site description"),
resource.TestCheckResourceAttr("netbox_site.test", "physical_address", "Physical address"),
resource.TestCheckResourceAttr("netbox_site.test", "shipping_address", "Shipping address"),
)},
{
Config: fmt.Sprintf(`
Expand All @@ -156,6 +165,8 @@ resource "netbox_site" "test" {
}`, testField, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_site.test", "description", ""),
resource.TestCheckResourceAttr("netbox_site.test", "physical_address", ""),
resource.TestCheckResourceAttr("netbox_site.test", "shipping_address", ""),
),
},
},
Expand Down