Skip to content

Commit

Permalink
Merge pull request #113 from aaronmaxlevy/add_support_for_blocks
Browse files Browse the repository at this point in the history
Adding support for IPv4 Block resources
  • Loading branch information
adarobin authored Nov 21, 2024
2 parents 4feea58 + 0e35fe1 commit 0f93ddc
Show file tree
Hide file tree
Showing 6 changed files with 1,176 additions and 0 deletions.
61 changes: 61 additions & 0 deletions docs/resources/ip4_block.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "bluecat_ip4_block Resource - terraform-provider-bluecat"
subcategory: ""
description: |-
Resource to create an IPv4 block.
---

# bluecat_ip4_block (Resource)

Resource to create an IPv4 block.

## Example Usage

```terraform
resource "bluecat_ip4_block" "block" {
parent_id = data.bluecat_ip4_network-block-range.block.id
name = "New Block"
size = 256
}
output "bluecat_ip4_block_cidr" {
value = bluecat_ip4_block.block.cidr
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `parent_id` (Number) The object ID of the parent object that will contain the new IPv4 block. If this argument is changed, then the resource will be recreated.
- `size` (Number) The size of the IPv4 block expressed as a power of 2. For example, 256 would create a /24. If this argument is changed, then the resource will be recreated.

### Optional

- `allow_duplicate_host` (Boolean) Duplicate host names check.
- `default_domains` (Set of Number) The object ids of the default DNS domains.
- `default_view` (Number) The object id of the default DNS View for the block.
- `dns_restrictions` (Set of Number) The object ids of the DNS restrictions for the block.
- `inherit_allow_duplicate_host` (Boolean) Duplicate host names check is inherited.
- `inherit_default_domains` (Boolean) Default domains are inherited.
- `inherit_default_view` (Boolean) The default DNS View is inherited.
- `inherit_dns_restrictions` (Boolean) DNS restrictions are inherited.
- `inherit_ping_before_assign` (Boolean) PingBeforeAssign option inheritance check option property.
- `is_larger_allowed` (Boolean) (Optional) Is it ok to return a block that is larger than the size specified?
- `location_code` (String) The location code of the block.
- `name` (String) The display name of the IPv4 block.
- `ping_before_assign` (Boolean) Option to ping check. The possible values are enable and disable.
- `traversal_method` (String) The traversal method used to find the range to allocate the block. Must be one of "NO_TRAVERSAL", "DEPTH_FIRST", or "BREADTH_FIRST".
- `user_defined_fields` (Map of String) A map of all user-definied fields associated with the IP4 Block.

### Read-Only

- `cidr` (String) The CIDR value of the block (if it forms a valid CIDR).
- `end` (String) The end of the block (if it does not form a valid CIDR).
- `id` (String) IPv4 Block identifier.
- `location_inherited` (Boolean) The location is inherited.
- `properties` (String) The properties of the resource as returned by the API (pipe delimited).
- `start` (String) The start of the block (if it does not form a valid CIDR).
- `type` (String) The type of the resource.
9 changes: 9 additions & 0 deletions examples/resources/bluecat_ip4_block/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resource "bluecat_ip4_block" "block" {
parent_id = data.bluecat_ip4_network-block-range.block.id
name = "New Block"
size = 256
}

output "bluecat_ip4_block_cidr" {
value = bluecat_ip4_block.block.cidr
}
180 changes: 180 additions & 0 deletions internal/provider/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,186 @@ func flattenIP4NetworkProperties(e *gobam.APIEntity) (*IP4NetworkModel, diag.Dia
return i, d
}

// IP4BlockModel describes the data model the built-in properties for an IP4Block object.
type IP4BlockModel struct {
// These are exposed via the entity properties field for objects of type IP4Block
CIDR types.String
DefaultDomains types.Set
Start types.String
End types.String
DefaultView types.Int64
DNSRestrictions types.Set
AllowDuplicateHost types.Bool
PingBeforeAssign types.Bool
InheritAllowDuplicateHost types.Bool
InheritPingBeforeAssign types.Bool
InheritDNSRestrictions types.Bool
InheritDefaultDomains types.Bool
InheritDefaultView types.Bool
LocationCode types.String
LocationInherited types.Bool

// these are user defined fields that are not built-in
UserDefinedFields types.Map
}

func flattenIP4BlockProperties(e *gobam.APIEntity) (*IP4BlockModel, diag.Diagnostics) {
var d diag.Diagnostics

if e == nil {
d.AddError("invalid input to flattenIP4Block", "entity passed was nil")
return nil, d
}
if e.Type == nil {
d.AddError("invalid input to flattenIP4Block", "type of entity passed was nil")
return nil, d
} else if *e.Type != "IP4Block" {
d.AddError("invalid input to flattenIP4Block", fmt.Sprintf("type of entity passed was %s", *e.Type))
return nil, d
}

i := &IP4BlockModel{}
udfMap := make(map[string]attr.Value)

var defaultDomainsSet basetypes.SetValue
var dnsRestrictionsSet basetypes.SetValue
defaultDomainsFound := false
dnsRestrictionsFound := false

if e.Properties != nil {
props := strings.Split(*e.Properties, "|")
for x := range props {
if len(props[x]) > 0 {
prop := strings.Split(props[x], "=")[0]
val := strings.Split(props[x], "=")[1]

switch prop {
case "name":
// we ignore the name because it is already a top level parameter
case "CIDR":
i.CIDR = types.StringValue(val)
case "defaultDomains":
defaultDomainsFound = true
var ddDiag diag.Diagnostics
defaultDomains := strings.Split(val, ",")
defaultDomainsList := []attr.Value{}
for x := range defaultDomains {
dID, err := strconv.ParseInt(defaultDomains[x], 10, 64)
if err != nil {
d.AddError("error parsing defaultDomains to int64", err.Error())
break
}
defaultDomainsList = append(defaultDomainsList, types.Int64Value(dID))
}

defaultDomainsSet, ddDiag = basetypes.NewSetValue(types.Int64Type, defaultDomainsList)
if ddDiag.HasError() {
d.Append(ddDiag...)
break
}
case "start":
i.Start = types.StringValue(val)
case "end":
i.End = types.StringValue(val)
case "defaultView":
dv, err := strconv.ParseInt(val, 10, 64)
if err != nil {
d.AddError("error parsing defaultView to int64", err.Error())
break
}
i.DefaultView = types.Int64Value(dv)
case "dnsRestrictions":
dnsRestrictionsFound = true
var drDiag diag.Diagnostics
dnsRestrictions := strings.Split(val, ",")
didList := []attr.Value{}
for x := range dnsRestrictions {
dID, err := strconv.ParseInt(dnsRestrictions[x], 10, 64)
if err != nil {
d.AddError("error parsing dnsRestrictions to int64", err.Error())
break
}
didList = append(didList, types.Int64Value(dID))
}
dnsRestrictionsSet, drDiag = basetypes.NewSetValue(types.Int64Type, didList)
if drDiag.HasError() {
d.Append(drDiag...)
}
case "allowDuplicateHost":
i.AllowDuplicateHost = types.BoolPointerValue(enableDisableToBool(val))
case "pingBeforeAssign":
i.PingBeforeAssign = types.BoolPointerValue(enableDisableToBool(val))
case "inheritAllowDuplicateHost":
b, err := strconv.ParseBool(val)
if err != nil {
d.AddError("error parsing inheritAllowDuplicateHost to bool", err.Error())
break
}
i.InheritAllowDuplicateHost = types.BoolValue(b)
case "inheritPingBeforeAssign":
b, err := strconv.ParseBool(val)
if err != nil {
d.AddError("error parsing inheritPingBeforeAssign to bool", err.Error())
break
}
i.InheritPingBeforeAssign = types.BoolValue(b)
case "inheritDNSRestrictions":
b, err := strconv.ParseBool(val)
if err != nil {
d.AddError("error parsing inheritDNSRestrictions to bool", err.Error())
break
}
i.InheritDNSRestrictions = types.BoolValue(b)
case "inheritDefaultDomains":
b, err := strconv.ParseBool(val)
if err != nil {
d.AddError("error parsing inheritDefaultDomains to bool", err.Error())
break
}
i.InheritDefaultDomains = types.BoolValue(b)
case "inheritDefaultView":
b, err := strconv.ParseBool(val)
if err != nil {
d.AddError("error parsing inheritDefaultView to bool", err.Error())
break
}
i.InheritDefaultView = types.BoolValue(b)
case "locationCode":
i.LocationCode = types.StringValue(val)
case "locationInherited":
b, err := strconv.ParseBool(val)
if err != nil {
d.AddError("error parsing locationInherited to bool", err.Error())
break
}
i.LocationInherited = types.BoolValue(b)
default:
udfMap[prop] = types.StringValue(val)
}
}
}
}

if !dnsRestrictionsFound {
dnsRestrictionsSet = basetypes.NewSetNull(types.Int64Type)
}
i.DNSRestrictions = dnsRestrictionsSet

if !defaultDomainsFound {
defaultDomainsSet = basetypes.NewSetNull(types.Int64Type)
}
i.DefaultDomains = defaultDomainsSet

var userDefinedFields basetypes.MapValue
userDefinedFields, udfDiag := basetypes.NewMapValue(types.StringType, udfMap)
if udfDiag.HasError() {
d.Append(udfDiag...)
}
i.UserDefinedFields = userDefinedFields

return i, d
}

func enableDisableToBool(s string) *bool {
var val *bool

Expand Down
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ func (p *blueCatProvider) Resources(ctx context.Context) []func() resource.Resou
NewIP4AddressResource,
NewIP4NetworkResource,
NewIP4AvailableNetworkResource,
NewIP4BlockResource,
}
}

Expand Down
Loading

0 comments on commit 0f93ddc

Please sign in to comment.