From 10047f38ae52968f7b88190c5b5342ad571b3fd8 Mon Sep 17 00:00:00 2001 From: Brandon Beck Date: Fri, 6 May 2022 07:49:47 -0500 Subject: [PATCH] adding address_list attribute to ranges --- .gitignore | 3 +- .goreleaser.yml | 2 +- docs/data-sources/range.md | 3 +- docs/resources/range.md | 1 + go.mod | 2 +- go.sum | 4 +- infoblox/data_range.go | 8 +++ infoblox/resource_range.go | 9 +++ .../techBeck03/infoblox-go-sdk/.gitignore | 2 + .../techBeck03/infoblox-go-sdk/client.go | 4 +- .../techBeck03/infoblox-go-sdk/go.mod | 5 ++ .../techBeck03/infoblox-go-sdk/go.sum | 2 + .../techBeck03/infoblox-go-sdk/ipv4address.go | 22 ++++---- .../techBeck03/infoblox-go-sdk/range.go | 56 +++++++++++++++---- .../techBeck03/infoblox-go-sdk/types.go | 1 + vendor/modules.txt | 2 +- 16 files changed, 97 insertions(+), 29 deletions(-) create mode 100644 vendor/github.com/techBeck03/infoblox-go-sdk/.gitignore create mode 100644 vendor/github.com/techBeck03/infoblox-go-sdk/go.mod create mode 100644 vendor/github.com/techBeck03/infoblox-go-sdk/go.sum diff --git a/.gitignore b/.gitignore index 24c2f39..151ad05 100644 --- a/.gitignore +++ b/.gitignore @@ -40,4 +40,5 @@ terraform .env -dist/ \ No newline at end of file +dist/ +test/ \ No newline at end of file diff --git a/.goreleaser.yml b/.goreleaser.yml index 92f80f6..a5a9236 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -47,7 +47,7 @@ signs: - "${signature}" - "--detach-sign" - "${artifact}" -release: +# release: # If you want to manually examine the release before its live, uncomment this line: # draft: true changelog: diff --git a/docs/data-sources/range.md b/docs/data-sources/range.md index 8df97d2..93e7b7e 100644 --- a/docs/data-sources/range.md +++ b/docs/data-sources/range.md @@ -21,7 +21,7 @@ data "infoblox_range" "range" { ```terraform data "infoblox_range" "range" { - ref = "range/867530986753098675309867530986753098675309867530986753098675309:172.19.4.2/172.19.4.10/default/default"no + ref = "range/867530986753098675309867530986753098675309867530986753098675309:172.19.4.2/172.19.4.10/default/default" } ``` @@ -29,6 +29,7 @@ data "infoblox_range" "range" { The following attributes are exported. +- `address_list` - (Computed) The list of IP Addresses associated with this range - `cidr` - (MutuallyExclusiveGroup*/Computed, String) The network to which this range belongs, in IPv4 Address/CIDR format. - `comment` - (Computed, String) Comment for the range; maximum 256 characters. - `disable_dhcp` - (Computed, Bool) Disable for DHCP. diff --git a/docs/resources/range.md b/docs/resources/range.md index 5034206..67fae2b 100644 --- a/docs/resources/range.md +++ b/docs/resources/range.md @@ -69,6 +69,7 @@ resource "infoblox_range" "specified_range" { The following attributes are exported. +- `address_list` - (Computed) The list of IP Addresses associated with this range - `cidr` - (Required, String) The network to which this range belongs, in IPv4 Address/CIDR format. - `comment` - (Optional, String) Comment for the range; maximum 256 characters. - `disable_dhcp` - (Optional, Bool) Disable for DHCP. diff --git a/go.mod b/go.mod index 1c16b71..f39b430 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,6 @@ require ( github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 github.com/hashicorp/terraform-plugin-sdk/v2 v2.4.3 github.com/techBeck03/go-ipmath v0.0.8 - github.com/techBeck03/infoblox-go-sdk v1.0.1 + github.com/techBeck03/infoblox-go-sdk v1.0.7 github.com/tidwall/gjson v1.9.3 ) diff --git a/go.sum b/go.sum index 063e4a4..f9c84f8 100644 --- a/go.sum +++ b/go.sum @@ -273,8 +273,8 @@ github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/techBeck03/go-ipmath v0.0.8 h1:U/z7bYt+92I/VpbJvpW48+hnPC0rOmvLuyEuiURNeWw= github.com/techBeck03/go-ipmath v0.0.8/go.mod h1:VugtTa3vBBdfSTeYQQov/NzzXt40R+LtuFr15KWUhpY= -github.com/techBeck03/infoblox-go-sdk v1.0.1 h1:Em+53efxhKUmB05+QbQHQdWJIpaljp7On+8K21ApekI= -github.com/techBeck03/infoblox-go-sdk v1.0.1/go.mod h1:miTvClw8XenPYZ9psw+71AmtlPmY2Lu0EPLBWJ0JIQw= +github.com/techBeck03/infoblox-go-sdk v1.0.7 h1:cH5F4QziYDuzA9i7KAl4CTMiuerWJzFB2dh8CRnbtQE= +github.com/techBeck03/infoblox-go-sdk v1.0.7/go.mod h1:hsKceL3PHHxRlGbOvZnsjC3KZ9bPCzCgaFWrWSSAUEA= github.com/tidwall/gjson v1.9.3 h1:hqzS9wAHMO+KVBBkLxYdkEeeFHuqr95GfClRLKlgK0E= github.com/tidwall/gjson v1.9.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= diff --git a/infoblox/data_range.go b/infoblox/data_range.go index e89e3e7..31c2009 100644 --- a/infoblox/data_range.go +++ b/infoblox/data_range.go @@ -20,6 +20,14 @@ func dataSourceRange() *schema.Resource { return &schema.Resource{ ReadContext: dataSourceRangeRead, Schema: map[string]*schema.Schema{ + "address_list": { + Type: schema.TypeList, + Description: "The list of IP Addresses associated with this range", + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, "cidr": { Type: schema.TypeString, Description: "The network to which this range belongs, in IPv4 Address/CIDR format.", diff --git a/infoblox/resource_range.go b/infoblox/resource_range.go index fdc882f..2e7496e 100644 --- a/infoblox/resource_range.go +++ b/infoblox/resource_range.go @@ -38,6 +38,14 @@ func resourceRange() *schema.Resource { makeGTIPCheck("start_address", "end_address"), ), Schema: map[string]*schema.Schema{ + "address_list": { + Type: schema.TypeList, + Description: "The list of IP Addresses associated with this range", + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, "cidr": { Type: schema.TypeString, Description: "The network to which this range belongs, in IPv4 Address/CIDR format.", @@ -217,6 +225,7 @@ func convertRangeToResourceData(client *infoblox.Client, d *schema.ResourceData, d.Set("range_function_string", fmt.Sprintf("%s-%s", addressRange.StartAddress, addressRange.EndAddress)) d.Set("network_view", addressRange.NetworkView) d.Set("disable_dhcp", addressRange.DisableDHCP) + d.Set("address_list", addressRange.IPAddressList) var memberList []map[string]interface{} if addressRange.Member != nil { diff --git a/vendor/github.com/techBeck03/infoblox-go-sdk/.gitignore b/vendor/github.com/techBeck03/infoblox-go-sdk/.gitignore new file mode 100644 index 0000000..a7dfb30 --- /dev/null +++ b/vendor/github.com/techBeck03/infoblox-go-sdk/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +.env \ No newline at end of file diff --git a/vendor/github.com/techBeck03/infoblox-go-sdk/client.go b/vendor/github.com/techBeck03/infoblox-go-sdk/client.go index 6640360..daaa447 100644 --- a/vendor/github.com/techBeck03/infoblox-go-sdk/client.go +++ b/vendor/github.com/techBeck03/infoblox-go-sdk/client.go @@ -8,6 +8,7 @@ import ( "io" "net/http" "net/url" + "sync" ) // Config - Configuration details for connecting to infoblox @@ -28,6 +29,7 @@ type Client struct { cookies []*http.Cookie eaDefinitions []EADefinition OrchestratorEAs *ExtensibleAttribute + SequentialLock sync.Mutex } // New - creates a new infoblox client @@ -108,7 +110,7 @@ func (c *Client) Call(request *http.Request, result interface{}) *ResponseError StatusCode: response.StatusCode, Request: fmt.Sprintf("%+v", request), ResponseBody: fmt.Sprintf("%+v", responseBody), - ErrorMessage: fmt.Sprintf("Request %+v\n failed with status code %d\n response %+v\n%+v", request, + ErrorMessage: fmt.Sprintf("Request %+v\n failed with status code %d\n response %+v", request, response.StatusCode, responseBody), } } diff --git a/vendor/github.com/techBeck03/infoblox-go-sdk/go.mod b/vendor/github.com/techBeck03/infoblox-go-sdk/go.mod new file mode 100644 index 0000000..8a3d7a9 --- /dev/null +++ b/vendor/github.com/techBeck03/infoblox-go-sdk/go.mod @@ -0,0 +1,5 @@ +module github.com/techBeck03/infoblox-go-sdk + +go 1.18 + +require github.com/techBeck03/go-ipmath v0.0.8 diff --git a/vendor/github.com/techBeck03/infoblox-go-sdk/go.sum b/vendor/github.com/techBeck03/infoblox-go-sdk/go.sum new file mode 100644 index 0000000..cdb7e8b --- /dev/null +++ b/vendor/github.com/techBeck03/infoblox-go-sdk/go.sum @@ -0,0 +1,2 @@ +github.com/techBeck03/go-ipmath v0.0.8 h1:U/z7bYt+92I/VpbJvpW48+hnPC0rOmvLuyEuiURNeWw= +github.com/techBeck03/go-ipmath v0.0.8/go.mod h1:VugtTa3vBBdfSTeYQQov/NzzXt40R+LtuFr15KWUhpY= diff --git a/vendor/github.com/techBeck03/infoblox-go-sdk/ipv4address.go b/vendor/github.com/techBeck03/infoblox-go-sdk/ipv4address.go index 318d4d3..0d5fbc5 100644 --- a/vendor/github.com/techBeck03/infoblox-go-sdk/ipv4address.go +++ b/vendor/github.com/techBeck03/infoblox-go-sdk/ipv4address.go @@ -55,10 +55,10 @@ func (c *Client) GetSequentialAddressRange(query AddressQuery) (*[]IPv4Address, if err != nil { return &addresses, err } - for matchFlag == false { + for !matchFlag { resultsCount := len(ret.Results) if ret.NextPageID == "" && ((len(prevPage) == 0 && resultsCount < query.Count) || (len(prevPage) > 0 && ((len(prevPage)-startIndex)+resultsCount) < query.Count)) { - return &addresses, fmt.Errorf("No sequential block found for supplied count") + return &addresses, fmt.Errorf("no sequential block found for supplied count") } if startIndex == -1 { startIndex = 0 @@ -66,7 +66,7 @@ func (c *Client) GetSequentialAddressRange(query AddressQuery) (*[]IPv4Address, } else { endIndex = 0 } - for endIndex <= resultsCount && matchFlag == false { + for endIndex <= resultsCount && !matchFlag { var currentMatch ipmath.IP var lastMatch ipmath.IP @@ -88,19 +88,19 @@ func (c *Client) GetSequentialAddressRange(query AddressQuery) (*[]IPv4Address, if currentMatch.Difference(lastMatch.Address) == (query.Count - 1) { if len(rangePage.Results) > 0 { - for rangeMatchFlag == false { + for !rangeMatchFlag { for _, addressRange := range rangePage.Results { if ipWithinRange(addressRange.StartAddress, addressRange.EndAddress, currentMatch.Address.String()) || ipWithinRange(addressRange.StartAddress, addressRange.EndAddress, lastMatch.Address.String()) { rangeMatchFlag = true break } } - if rangeMatchFlag == false && rangePage.NextPageID != "" { + if !rangeMatchFlag && rangePage.NextPageID != "" { rangePage, err = c.GetPaginatedCidrRanges(query.CIDR, rangePage.NextPageID) if err != nil { return &addresses, err } - } else if rangeMatchFlag == false && rangePage.NextPageID == "" { + } else if !rangeMatchFlag && rangePage.NextPageID == "" { matchFlag = true break } @@ -108,7 +108,7 @@ func (c *Client) GetSequentialAddressRange(query AddressQuery) (*[]IPv4Address, } else { matchFlag = true } - if matchFlag == true { + if matchFlag { for i := 0; i <= query.Count-1; i++ { if startIndex > endIndex { addresses = append(addresses, prevPage[startIndex]) @@ -130,7 +130,7 @@ func (c *Client) GetSequentialAddressRange(query AddressQuery) (*[]IPv4Address, } endIndex++ } - if matchFlag == false && ret.NextPageID != "" { + if !matchFlag && ret.NextPageID != "" { prevPage = ret.Results queryParams["_page_id"] = ret.NextPageID queryParamString := c.BuildQuery(queryParams) @@ -144,8 +144,8 @@ func (c *Client) GetSequentialAddressRange(query AddressQuery) (*[]IPv4Address, if response != nil { return &addresses, fmt.Errorf(response.ErrorMessage) } - } else if matchFlag == false && ret.NextPageID == "" { - return &addresses, fmt.Errorf("No sequential block found for supplied count") + } else if !matchFlag && ret.NextPageID == "" { + return &addresses, fmt.Errorf("no sequential block found for supplied count") } } return &addresses, nil @@ -176,7 +176,7 @@ func (c *Client) GetUsedAddressesWithinRange(query AddressQuery) (*[]IPv4Address return &addresses, fmt.Errorf(response.ErrorMessage) } var filteredResults []IPv4Address - if *query.FilterEmptyHostnames == true { + if *query.FilterEmptyHostnames { for _, result := range ret.Results { if (len(result.Hostnames) > 0 || len(result.Objects) > 0) && result.Status == "USED" { filteredResults = append(filteredResults, result) diff --git a/vendor/github.com/techBeck03/infoblox-go-sdk/range.go b/vendor/github.com/techBeck03/infoblox-go-sdk/range.go index e813d07..45b4a84 100644 --- a/vendor/github.com/techBeck03/infoblox-go-sdk/range.go +++ b/vendor/github.com/techBeck03/infoblox-go-sdk/range.go @@ -3,8 +3,11 @@ package infoblox import ( "fmt" "log" + "net" "net/http" "time" + + "github.com/techBeck03/go-ipmath" ) const ( @@ -35,10 +38,16 @@ func (c *Client) GetRangeByRef(ref string, queryParams map[string]string) (Range return ret, fmt.Errorf(response.ErrorMessage) } + startingIP := ipmath.IP{ + Address: net.ParseIP(ret.StartAddress), + } + count := startingIP.Difference(net.ParseIP(ret.EndAddress)) + 1 + ret.IPAddressList = getRangeAddressList(ret.StartAddress, count) + return ret, nil } -// GetRangeByQuery gets range by reference +// GetRangeByQuery gets range by query func (c *Client) GetRangeByQuery(queryParams map[string]string) ([]Range, error) { var ret RangeQueryResult @@ -58,9 +67,29 @@ func (c *Client) GetRangeByQuery(queryParams map[string]string) ([]Range, error) return nil, fmt.Errorf(response.ErrorMessage) } + for i, r := range ret.Results { + startingIP := ipmath.IP{ + Address: net.ParseIP(r.StartAddress), + } + count := startingIP.Difference(net.ParseIP(r.EndAddress)) + 1 + ret.Results[i].IPAddressList = getRangeAddressList(r.StartAddress, count) + } + return ret.Results, nil } +func getRangeAddressList(startAddress string, count int) []string { + ipAddressList := []string{} + startingIP := ipmath.IP{ + Address: net.ParseIP(startAddress), + } + for i := 0; i < count; i++ { + ipAddressList = append(ipAddressList, startingIP.ToIPString()) + startingIP.Inc() + } + return ipAddressList +} + // GetPaginatedCidrRanges gets ranges within CIDR by page func (c *Client) GetPaginatedCidrRanges(cidr string, pageID string) (rangePage RangeQueryResult, err error) { var ret RangeQueryResult @@ -105,6 +134,11 @@ func (c *Client) CreateRange(rangeObject *Range) error { if response != nil { return fmt.Errorf(response.ErrorMessage) } + startingIP := ipmath.IP{ + Address: net.ParseIP(rangeObject.StartAddress), + } + count := startingIP.Difference(net.ParseIP(rangeObject.EndAddress)) + 1 + rangeObject.IPAddressList = getRangeAddressList(rangeObject.StartAddress, count) return nil } @@ -129,7 +163,7 @@ func (c *Client) UpdateRange(ref string, rangeObject Range) (Range, error) { // DeleteRange deletes range func (c *Client) DeleteRange(ref string) error { - request, err := c.CreateJSONRequest(http.MethodDelete, fmt.Sprintf("%s", ref), nil) + request, err := c.CreateJSONRequest(http.MethodDelete, ref, nil) if err != nil { return err } @@ -146,11 +180,12 @@ func (c *Client) DeleteRange(ref string) error { // CreateSequentialRange creates sequential address range func (c *Client) CreateSequentialRange(rangeObject *Range, query AddressQuery) error { + c.SequentialLock.Lock() + defer c.SequentialLock.Unlock() query.fillDefaults() retryCount := 0 verified := false - - for verified == false && retryCount <= query.Retries { + for !verified && retryCount <= query.Retries { sequentialAddresses, err := c.GetSequentialAddressRange(query) if err != nil { return err @@ -161,6 +196,7 @@ func (c *Client) CreateSequentialRange(rangeObject *Range, query AddressQuery) e err = c.CreateRange(rangeObject) if err != nil { verified = false + time.Sleep(1 * time.Second) } else { log.Println("Pausing for race condition checks") time.Sleep(1 * time.Second) @@ -188,10 +224,10 @@ func (c *Client) CreateSequentialRange(rangeObject *Range, query AddressQuery) e } } - if verified == false { - return fmt.Errorf("Unable to create sequential range within %s", query.CIDR) + if !verified { + return fmt.Errorf("unable to create sequential range within %s", query.CIDR) } - + rangeObject.IPAddressList = getRangeAddressList(rangeObject.StartAddress, query.Count) return nil } @@ -225,14 +261,14 @@ func (c *Client) CheckIfRangeContainsRange(query IPsWithinRangeQuery) (bool, err matchFlag := false - for matchFlag == false { + for !matchFlag { for _, addressRange := range ret.Results { if addressRange.Ref != query.Ref && (ipWithinRange(addressRange.StartAddress, addressRange.EndAddress, query.StartAddress) || ipWithinRange(addressRange.StartAddress, addressRange.EndAddress, query.EndAddress)) { matchFlag = true break } } - if matchFlag == false && ret.NextPageID != "" { + if !matchFlag && ret.NextPageID != "" { queryParams["_page_id"] = ret.NextPageID queryParamString := c.BuildQuery(queryParams) @@ -245,7 +281,7 @@ func (c *Client) CheckIfRangeContainsRange(query IPsWithinRangeQuery) (bool, err if response != nil { return true, fmt.Errorf(response.ErrorMessage) } - } else if matchFlag == false && ret.NextPageID == "" { + } else if !matchFlag && ret.NextPageID == "" { return false, nil } } diff --git a/vendor/github.com/techBeck03/infoblox-go-sdk/types.go b/vendor/github.com/techBeck03/infoblox-go-sdk/types.go index e665a4c..b83522a 100644 --- a/vendor/github.com/techBeck03/infoblox-go-sdk/types.go +++ b/vendor/github.com/techBeck03/infoblox-go-sdk/types.go @@ -242,6 +242,7 @@ type Range struct { ExtensibleAttributes *ExtensibleAttribute `json:"extattrs,omitempty"` ExtensibleAttributesAdd *ExtensibleAttribute `json:"extattrs+,omitempty"` ExtensibleAttributesRemove *ExtensibleAttribute `json:"extattrs-,omitempty"` + IPAddressList []string `json:"ip_address_list,omitempty"` } // RangeQueryResult object diff --git a/vendor/modules.txt b/vendor/modules.txt index 2452b69..556507b 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -174,7 +174,7 @@ github.com/oklog/run # github.com/techBeck03/go-ipmath v0.0.8 ## explicit github.com/techBeck03/go-ipmath -# github.com/techBeck03/infoblox-go-sdk v1.0.1 +# github.com/techBeck03/infoblox-go-sdk v1.0.7 ## explicit github.com/techBeck03/infoblox-go-sdk # github.com/tidwall/gjson v1.9.3