Skip to content

Commit

Permalink
Merge pull request #5 from umich-vci/ip4_available_network
Browse files Browse the repository at this point in the history
new resource ip4_available_network
  • Loading branch information
adarobin authored Dec 9, 2020
2 parents ffd823f + 5d740bb commit de7adfb
Show file tree
Hide file tree
Showing 16 changed files with 557 additions and 163 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.14
go-version: 1.15
-
name: Import GPG key
id: import_gpg
Expand Down
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
## 0.2.0 (Not yet released)
## 0.2.0 (December 9, 2020)

BREAKING CHANGES:

* resource/bluecat_ip4_address: Removed `parent_id_list` argument and `computed_parent_id` attribute.
`parent_id` argument is now required.
([#4](https://github.com/umich-vci/terraform-provider-bluecat/issues/4))

FEATURES:

* **New Resource:** `bluecat_ip4_available_network` ([#4](https://github.com/umich-vci/terraform-provider-bluecat/issues/4))

IMPROVEMENTS:

* Updated [gobam](https://github.com/umich-vci/gobam) to 20201026200032-5742f663694f and added a new
provider configuration argument `ssl_verify` to allow ignoring SSL certificate validation errors.
([#1](https://github.com/umich-vci/terraform-provider-bluecat/issues/1))

* Switched from Terraform SDK v1 to v2 ([#5](https://github.com/umich-vci/terraform-provider-bluecat/pull/5))

* Now building with go 1.15

## 0.1.0 (October 14, 2020)

First release of provider.
4 changes: 2 additions & 2 deletions bluecat/data_source_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"log"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/umich-vci/gobam"
)

Expand Down
2 changes: 1 addition & 1 deletion bluecat/data_source_host_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strconv"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/umich-vci/gobam"
)

Expand Down
2 changes: 1 addition & 1 deletion bluecat/data_source_ip4_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strconv"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/umich-vci/gobam"
)

Expand Down
4 changes: 2 additions & 2 deletions bluecat/data_source_ip4_network-block-range.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strconv"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/umich-vci/gobam"
)

Expand Down
12 changes: 6 additions & 6 deletions bluecat/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package bluecat
import (
"sync"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
Expand All @@ -19,7 +18,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
}

// Provider returns a terraform resource provider
func Provider() terraform.ResourceProvider {
func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"username": {
Expand Down Expand Up @@ -49,9 +48,10 @@ func Provider() terraform.ResourceProvider {
},
},
ResourcesMap: map[string]*schema.Resource{
"bluecat_host_record": resourceHostRecord(),
"bluecat_ip4_address": resourceIP4Address(),
"bluecat_ip4_network": resourceIP4Network(),
"bluecat_host_record": resourceHostRecord(),
"bluecat_ip4_address": resourceIP4Address(),
"bluecat_ip4_available_network": resourceIP4AvailableNetwork(),
"bluecat_ip4_network": resourceIP4Network(),
},
DataSourcesMap: map[string]*schema.Resource{
"bluecat_entity": dataSourceEntity(),
Expand Down
2 changes: 1 addition & 1 deletion bluecat/resource_host_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strconv"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/umich-vci/gobam"
)

Expand Down
83 changes: 4 additions & 79 deletions bluecat/resource_ip4_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"log"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/umich-vci/gobam"
)

Expand All @@ -24,14 +24,9 @@ func resourceIP4Address() *schema.Resource {
},
"parent_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Required: true,
ForceNew: true,
},
"parent_id_list": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"mac_address": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -74,10 +69,6 @@ func resourceIP4Address() *schema.Resource {
Type: schema.TypeMap,
Optional: true,
},
"computed_parent_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand All @@ -96,72 +87,7 @@ func resourceIP4AddressCreate(d *schema.ResourceData, meta interface{}) error {
return err
}

parentIDString := ""
if pid, ok := d.GetOk("parent_id"); ok {
parentIDString = pid.(string)

if _, ok := d.GetOk("parent_id_list"); ok {
err := gobam.LogoutClientWithError(client, "Cannot specify both parent_id and parent_id_list")
mutex.Unlock()
return err
}

} else {
if pidList, ok := d.GetOk("parent_id_list"); ok {
list := pidList.(*schema.Set).List()
freeAddressMap := make(map[string]int)
for i := range list {
id, err := strconv.ParseInt(list[i].(string), 10, 64)
if err = gobam.LogoutClientIfError(client, err, "Unable to convert parent_id from string to int64"); err != nil {
mutex.Unlock()
return err
}
resp, err := client.GetEntityById(id)
if err = gobam.LogoutClientIfError(client, err, "Failed to get IP4 Network by Id"); err != nil {
mutex.Unlock()
return err
}

networkProperties, err := gobam.ParseIP4NetworkProperties(*resp.Properties)
if err = gobam.LogoutClientIfError(client, err, "Error parsing IP4 network properties"); err != nil {
mutex.Unlock()
return err
}

_, addressesFree, err := getIP4NetworkAddressUsage(*resp.Id, networkProperties.CIDR, client)
if err = gobam.LogoutClientIfError(client, err, "Error calculating network usage"); err != nil {
mutex.Unlock()
return err
}

if addressesFree > 0 {
freeAddressMap[strconv.FormatInt(id, 10)] = addressesFree
}

}

parentIDMostFree := ""
freeCount := 0
for k, v := range freeAddressMap {
if v > freeCount {
freeCount = v
parentIDMostFree = k
}
}

if freeCount == 0 {
err := gobam.LogoutClientWithError(client, "No networks had a free address")
mutex.Unlock()
return err
}

parentIDString = parentIDMostFree
} else {
err := gobam.LogoutClientWithError(client, "One of parent_id or parent_id_list must be specified")
mutex.Unlock()
return err
}
}
parentIDString := d.Get("parent_id").(string)

parentID, err := strconv.ParseInt(parentIDString, 10, 64)
if err = gobam.LogoutClientIfError(client, err, "Unable to convert parent_id from string to int64"); err != nil {
Expand All @@ -188,7 +114,6 @@ func resourceIP4AddressCreate(d *schema.ResourceData, meta interface{}) error {
}

d.SetId(strconv.FormatInt(*resp.Id, 10))
d.Set("computed_parent_id", parentIDString)

// logout client
if err := client.Logout(); err != nil {
Expand Down
Loading

0 comments on commit de7adfb

Please sign in to comment.