|
| 1 | +package phpipam |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "strconv" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform/helper/schema" |
| 8 | + "github.com/paybyphone/phpipam-sdk-go/controllers/addresses" |
| 9 | + "github.com/paybyphone/phpipam-sdk-go/phpipam" |
| 10 | +) |
| 11 | + |
| 12 | +// resourceAddressOptionalFields represents all the fields that are optional in |
| 13 | +// the phpipam_address resource. These fields get flagged as Optional, with zero |
| 14 | +// value defaults (the field is not set), in addition to being marked as |
| 15 | +// Computed. Any field not listed here cannot be supplied by the resource and |
| 16 | +// is solely computed. |
| 17 | +var resourceAddressOptionalFields = linearSearchSlice{ |
| 18 | + "is_gateway", |
| 19 | + "description", |
| 20 | + "hostname", |
| 21 | + "mac_address", |
| 22 | + "owner", |
| 23 | + "state_tag_id", |
| 24 | + "skip_ptr_record", |
| 25 | + "ptr_record_id", |
| 26 | + "device_id", |
| 27 | + "switch_port_label", |
| 28 | + "note", |
| 29 | + "exclude_ping", |
| 30 | +} |
| 31 | + |
| 32 | +// bareAddressSchema returns a map[string]*schema.Schema with the schema used |
| 33 | +// to represent a PHPIPAM address resource. This output should then be modified |
| 34 | +// so that required and computed fields are set properly for both the data |
| 35 | +// source and the resource. |
| 36 | +func bareAddressSchema() map[string]*schema.Schema { |
| 37 | + return map[string]*schema.Schema{ |
| 38 | + "address_id": &schema.Schema{ |
| 39 | + Type: schema.TypeInt, |
| 40 | + }, |
| 41 | + "subnet_id": &schema.Schema{ |
| 42 | + Type: schema.TypeInt, |
| 43 | + }, |
| 44 | + "ip_address": &schema.Schema{ |
| 45 | + Type: schema.TypeString, |
| 46 | + }, |
| 47 | + "is_gateway": &schema.Schema{ |
| 48 | + Type: schema.TypeBool, |
| 49 | + }, |
| 50 | + "description": &schema.Schema{ |
| 51 | + Type: schema.TypeString, |
| 52 | + }, |
| 53 | + "hostname": &schema.Schema{ |
| 54 | + Type: schema.TypeString, |
| 55 | + }, |
| 56 | + "mac_address": &schema.Schema{ |
| 57 | + Type: schema.TypeString, |
| 58 | + }, |
| 59 | + "owner": &schema.Schema{ |
| 60 | + Type: schema.TypeString, |
| 61 | + }, |
| 62 | + "state_tag_id": &schema.Schema{ |
| 63 | + Type: schema.TypeInt, |
| 64 | + }, |
| 65 | + "skip_ptr_record": &schema.Schema{ |
| 66 | + Type: schema.TypeBool, |
| 67 | + }, |
| 68 | + "ptr_record_id": &schema.Schema{ |
| 69 | + Type: schema.TypeInt, |
| 70 | + }, |
| 71 | + "device_id": &schema.Schema{ |
| 72 | + Type: schema.TypeInt, |
| 73 | + }, |
| 74 | + "switch_port_label": &schema.Schema{ |
| 75 | + Type: schema.TypeString, |
| 76 | + }, |
| 77 | + "note": &schema.Schema{ |
| 78 | + Type: schema.TypeString, |
| 79 | + }, |
| 80 | + "last_seen": &schema.Schema{ |
| 81 | + Type: schema.TypeString, |
| 82 | + }, |
| 83 | + "exclude_ping": &schema.Schema{ |
| 84 | + Type: schema.TypeBool, |
| 85 | + }, |
| 86 | + "edit_date": &schema.Schema{ |
| 87 | + Type: schema.TypeString, |
| 88 | + }, |
| 89 | + "custom_fields": &schema.Schema{ |
| 90 | + Type: schema.TypeMap, |
| 91 | + }, |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// resourceAddressSchema returns the schema for the phpipam_address resource. |
| 96 | +// It sets the required and optional fields, the latter defined in |
| 97 | +// resourceAddressRequiredFields, and ensures that all optional and |
| 98 | +// non-configurable fields are computed as well. |
| 99 | +func resourceAddressSchema() map[string]*schema.Schema { |
| 100 | + s := bareAddressSchema() |
| 101 | + for k, v := range s { |
| 102 | + switch { |
| 103 | + // IP Address and Subnet ID are ForceNew |
| 104 | + case k == "subnet_id" || k == "ip_address": |
| 105 | + v.Required = true |
| 106 | + v.ForceNew = true |
| 107 | + case k == "custom_fields": |
| 108 | + v.Optional = true |
| 109 | + case resourceAddressOptionalFields.Has(k): |
| 110 | + v.Optional = true |
| 111 | + v.Computed = true |
| 112 | + default: |
| 113 | + v.Computed = true |
| 114 | + } |
| 115 | + } |
| 116 | + // Add the remove_dns_on_delete item to the schema. This is a meta-parameter |
| 117 | + // that is not part of the API resource and exists to instruct PHPIPAM to |
| 118 | + // gracefully remove the address from its DNS integrations as well when it is |
| 119 | + // removed. The default on this option is true. |
| 120 | + s["remove_dns_on_delete"] = &schema.Schema{ |
| 121 | + Type: schema.TypeBool, |
| 122 | + Optional: true, |
| 123 | + Default: true, |
| 124 | + } |
| 125 | + return s |
| 126 | +} |
| 127 | + |
| 128 | +// dataSourceAddressSchema returns the schema for the phpipam_address data |
| 129 | +// source. It sets the searchable fields and sets up the attribute conflicts |
| 130 | +// between IP address and address ID. It also ensures that all fields are |
| 131 | +// computed as well. |
| 132 | +func dataSourceAddressSchema() map[string]*schema.Schema { |
| 133 | + s := bareAddressSchema() |
| 134 | + for k, v := range s { |
| 135 | + switch k { |
| 136 | + case "address_id": |
| 137 | + v.Optional = true |
| 138 | + v.Computed = true |
| 139 | + v.ConflictsWith = []string{"ip_address", "subnet_id", "description", "hostname", "custom_field_filter"} |
| 140 | + case "ip_address": |
| 141 | + v.Optional = true |
| 142 | + v.Computed = true |
| 143 | + v.ConflictsWith = []string{"address_id", "subnet_id", "description", "hostname", "custom_field_filter"} |
| 144 | + case "subnet_id": |
| 145 | + v.Optional = true |
| 146 | + v.Computed = true |
| 147 | + v.ConflictsWith = []string{"ip_address", "address_id"} |
| 148 | + case "description": |
| 149 | + v.Optional = true |
| 150 | + v.Computed = true |
| 151 | + v.ConflictsWith = []string{"ip_address", "address_id", "hostname", "custom_field_filter"} |
| 152 | + case "hostname": |
| 153 | + v.Optional = true |
| 154 | + v.Computed = true |
| 155 | + v.ConflictsWith = []string{"ip_address", "address_id", "description", "custom_field_filter"} |
| 156 | + default: |
| 157 | + v.Computed = true |
| 158 | + } |
| 159 | + } |
| 160 | + // Add the custom_field_filter item to the schema. This is a meta-parameter |
| 161 | + // that allows searching for a custom field value in the data source. |
| 162 | + s["custom_field_filter"] = customFieldFilterSchema([]string{"ip_address", "address_id", "hostname", "description"}) |
| 163 | + |
| 164 | + return s |
| 165 | +} |
| 166 | + |
| 167 | +// expandAddress returns the addresses.Address structure for a |
| 168 | +// phpiapm_address resource or data source. Depending on if we are dealing with |
| 169 | +// the resource or data source, extra considerations may need to be taken. |
| 170 | +func expandAddress(d *schema.ResourceData) addresses.Address { |
| 171 | + s := addresses.Address{ |
| 172 | + ID: d.Get("address_id").(int), |
| 173 | + SubnetID: d.Get("subnet_id").(int), |
| 174 | + IPAddress: d.Get("ip_address").(string), |
| 175 | + IsGateway: phpipam.BoolIntString(d.Get("is_gateway").(bool)), |
| 176 | + Description: d.Get("description").(string), |
| 177 | + Hostname: d.Get("hostname").(string), |
| 178 | + MACAddress: d.Get("mac_address").(string), |
| 179 | + Owner: d.Get("owner").(string), |
| 180 | + Tag: d.Get("state_tag_id").(int), |
| 181 | + PTRIgnore: phpipam.BoolIntString(d.Get("skip_ptr_record").(bool)), |
| 182 | + PTRRecordID: d.Get("ptr_record_id").(int), |
| 183 | + DeviceID: d.Get("device_id").(int), |
| 184 | + Port: d.Get("switch_port_label").(string), |
| 185 | + Note: d.Get("note").(string), |
| 186 | + LastSeen: d.Get("last_seen").(string), |
| 187 | + ExcludePing: phpipam.BoolIntString(d.Get("exclude_ping").(bool)), |
| 188 | + } |
| 189 | + |
| 190 | + return s |
| 191 | +} |
| 192 | + |
| 193 | +// flattenAddress sets fields in a *schema.ResourceData with fields supplied by |
| 194 | +// the input addresses.Address. This is used in read operations. |
| 195 | +func flattenAddress(a addresses.Address, d *schema.ResourceData) { |
| 196 | + d.SetId(strconv.Itoa(a.ID)) |
| 197 | + d.Set("address_id", a.ID) |
| 198 | + d.Set("subnet_id", a.SubnetID) |
| 199 | + d.Set("ip_address", a.IPAddress) |
| 200 | + d.Set("is_gateway", a.IsGateway) |
| 201 | + d.Set("description", a.Description) |
| 202 | + d.Set("hostname", a.Hostname) |
| 203 | + d.Set("mac_address", a.MACAddress) |
| 204 | + d.Set("owner", a.Owner) |
| 205 | + d.Set("state_tag_id", a.Tag) |
| 206 | + d.Set("skip_ptr_record", a.PTRIgnore) |
| 207 | + d.Set("ptr_record_id", a.PTRRecordID) |
| 208 | + d.Set("device_id", a.DeviceID) |
| 209 | + d.Set("switch_port_label", a.Port) |
| 210 | + d.Set("note", a.Note) |
| 211 | + d.Set("last_seen", a.LastSeen) |
| 212 | + d.Set("exclude_ping", a.ExcludePing) |
| 213 | + d.Set("edit_date", a.EditDate) |
| 214 | +} |
| 215 | + |
| 216 | +// addressSearchInSubnet provides the address search functionality for both the |
| 217 | +// phpipam_address and phpipam_addresses data sources, returning an |
| 218 | +// []addresses.Address to the particular data source that is calling the |
| 219 | +// function. From here it's up to the specific data source to determine what |
| 220 | +// they want to do with the results (ie: reject it on matching nothing or more |
| 221 | +// than one for the singular data source, or extracting the IDs for the plural |
| 222 | +// one). |
| 223 | +func addressSearchInSubnet(d *schema.ResourceData, meta interface{}) ([]addresses.Address, error) { |
| 224 | + c := meta.(*ProviderPHPIPAMClient).addressesController |
| 225 | + s := meta.(*ProviderPHPIPAMClient).subnetsController |
| 226 | + result := make([]addresses.Address, 0) |
| 227 | + v, err := s.GetAddressesInSubnet(d.Get("subnet_id").(int)) |
| 228 | + if err != nil { |
| 229 | + return result, err |
| 230 | + } |
| 231 | + if len(v) == 0 { |
| 232 | + return result, errors.New("No addresses were found in the supplied subnet") |
| 233 | + } |
| 234 | + for _, r := range v { |
| 235 | + switch { |
| 236 | + // Double-assert that we don't have empty strings in the conditionals |
| 237 | + // to ensure there there is no edge cases with matching zero values. |
| 238 | + case d.Get("description").(string) != "" && r.Description == d.Get("description").(string): |
| 239 | + result = append(result, r) |
| 240 | + case d.Get("hostname").(string) != "" && r.Hostname == d.Get("hostname").(string): |
| 241 | + result = append(result, r) |
| 242 | + case len(d.Get("custom_field_filter").(map[string]interface{})) > 0: |
| 243 | + fields, err := c.GetAddressCustomFields(r.ID) |
| 244 | + if err != nil { |
| 245 | + return result, err |
| 246 | + } |
| 247 | + search := d.Get("custom_field_filter").(map[string]interface{}) |
| 248 | + matched, err := customFieldFilter(fields, search) |
| 249 | + if err != nil { |
| 250 | + return result, err |
| 251 | + } |
| 252 | + if matched { |
| 253 | + result = append(result, r) |
| 254 | + } |
| 255 | + } |
| 256 | + } |
| 257 | + return result, nil |
| 258 | +} |
0 commit comments