|
| 1 | +package host |
| 2 | + |
| 3 | +import ( |
| 4 | + "regexp" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 11 | + "github.com/joneshf/terraform-provider-openwrt/lucirpc" |
| 12 | + "github.com/joneshf/terraform-provider-openwrt/openwrt/internal/lucirpcglue" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + addDNSEntriesAttribute = "dns" |
| 17 | + addDNSEntriesAttributeDescription = "Add static forward and reverse DNS entries for this host." |
| 18 | + addDNSEntriesUCIOption = "dns" |
| 19 | + |
| 20 | + hostnameAttribute = "name" |
| 21 | + hostnameAttributeDescription = "Hostname to assign." |
| 22 | + hostnameUCIOption = "name" |
| 23 | + |
| 24 | + ipAddressAttribute = "ip" |
| 25 | + ipAddressAttributeDescription = "The IP address to be used for this host, or `ignore` to ignore any DHCP request from this host." |
| 26 | + ipAddressUCIOption = "ip" |
| 27 | + |
| 28 | + macAddressAttribute = "mac" |
| 29 | + macAddressAttributeDescription = "The hardware address(es) of this host, separated by spaces." |
| 30 | + macAddressUCIOption = "mac" |
| 31 | + |
| 32 | + schemaDescription = "Assign a fixed IP address to hosts." |
| 33 | + |
| 34 | + uciConfig = "dhcp" |
| 35 | + uciType = "host" |
| 36 | +) |
| 37 | + |
| 38 | +var ( |
| 39 | + addDNSEntriesSchemaAttribute = lucirpcglue.BoolSchemaAttribute[model, lucirpc.Options, lucirpc.Options]{ |
| 40 | + Description: addDNSEntriesAttributeDescription, |
| 41 | + ReadResponse: lucirpcglue.ReadResponseOptionBool(modelSetAddDNSEntries, addDNSEntriesAttribute, addDNSEntriesUCIOption), |
| 42 | + ResourceExistence: lucirpcglue.NoValidation, |
| 43 | + UpsertRequest: lucirpcglue.UpsertRequestOptionBool(modelGetAddDNSEntries, addDNSEntriesAttribute, addDNSEntriesUCIOption), |
| 44 | + } |
| 45 | + |
| 46 | + hostnameSchemaAttribute = lucirpcglue.StringSchemaAttribute[model, lucirpc.Options, lucirpc.Options]{ |
| 47 | + Description: hostnameAttributeDescription, |
| 48 | + ReadResponse: lucirpcglue.ReadResponseOptionString(modelSetHostname, hostnameAttribute, hostnameUCIOption), |
| 49 | + ResourceExistence: lucirpcglue.NoValidation, |
| 50 | + UpsertRequest: lucirpcglue.UpsertRequestOptionString(modelGetHostname, hostnameAttribute, hostnameUCIOption), |
| 51 | + } |
| 52 | + |
| 53 | + ipAddressSchemaAttribute = lucirpcglue.StringSchemaAttribute[model, lucirpc.Options, lucirpc.Options]{ |
| 54 | + Description: ipAddressAttributeDescription, |
| 55 | + ReadResponse: lucirpcglue.ReadResponseOptionString(modelSetIPAddress, ipAddressAttribute, ipAddressUCIOption), |
| 56 | + ResourceExistence: lucirpcglue.NoValidation, |
| 57 | + UpsertRequest: lucirpcglue.UpsertRequestOptionString(modelGetIPAddress, ipAddressAttribute, ipAddressUCIOption), |
| 58 | + Validators: []validator.String{ |
| 59 | + stringvalidator.Any( |
| 60 | + stringvalidator.OneOf( |
| 61 | + "ignore", |
| 62 | + ), |
| 63 | + stringvalidator.RegexMatches( |
| 64 | + regexp.MustCompile("^([[:digit:]]{1,3}.){3}[[:digit:]]{1,3}$"), |
| 65 | + `must be a valid IP address (e.g. "192.168.3.1")`, |
| 66 | + ), |
| 67 | + ), |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + macAddressSchemaAttribute = lucirpcglue.StringSchemaAttribute[model, lucirpc.Options, lucirpc.Options]{ |
| 72 | + Description: macAddressAttributeDescription, |
| 73 | + ReadResponse: lucirpcglue.ReadResponseOptionString(modelSetMACAddress, macAddressAttribute, macAddressUCIOption), |
| 74 | + ResourceExistence: lucirpcglue.NoValidation, |
| 75 | + UpsertRequest: lucirpcglue.UpsertRequestOptionString(modelGetMACAddress, macAddressAttribute, macAddressUCIOption), |
| 76 | + Validators: []validator.String{ |
| 77 | + stringvalidator.RegexMatches( |
| 78 | + regexp.MustCompile("^([[:xdigit:]][[:xdigit:]]:){5}[[:xdigit:]][[:xdigit:]]$"), |
| 79 | + `must be a valid MAC address (e.g. "12:34:56:78:90:ab")`, |
| 80 | + ), |
| 81 | + }, |
| 82 | + } |
| 83 | + |
| 84 | + schemaAttributes = map[string]lucirpcglue.SchemaAttribute[model, lucirpc.Options, lucirpc.Options]{ |
| 85 | + addDNSEntriesAttribute: addDNSEntriesSchemaAttribute, |
| 86 | + hostnameAttribute: hostnameSchemaAttribute, |
| 87 | + ipAddressAttribute: ipAddressSchemaAttribute, |
| 88 | + lucirpcglue.IdAttribute: lucirpcglue.IdSchemaAttribute(modelGetId, modelSetId), |
| 89 | + macAddressAttribute: macAddressSchemaAttribute, |
| 90 | + } |
| 91 | +) |
| 92 | + |
| 93 | +func NewDataSource() datasource.DataSource { |
| 94 | + return lucirpcglue.NewDataSource( |
| 95 | + modelGetId, |
| 96 | + schemaAttributes, |
| 97 | + schemaDescription, |
| 98 | + uciConfig, |
| 99 | + uciType, |
| 100 | + ) |
| 101 | +} |
| 102 | + |
| 103 | +func NewResource() resource.Resource { |
| 104 | + return lucirpcglue.NewResource( |
| 105 | + modelGetId, |
| 106 | + schemaAttributes, |
| 107 | + schemaDescription, |
| 108 | + uciConfig, |
| 109 | + uciType, |
| 110 | + ) |
| 111 | +} |
| 112 | + |
| 113 | +type model struct { |
| 114 | + AddDNSEntries types.Bool `tfsdk:"dns"` |
| 115 | + Hostname types.String `tfsdk:"name"` |
| 116 | + Id types.String `tfsdk:"id"` |
| 117 | + IPAddress types.String `tfsdk:"ip"` |
| 118 | + MACAddress types.String `tfsdk:"mac"` |
| 119 | +} |
| 120 | + |
| 121 | +func modelGetAddDNSEntries(m model) types.Bool { return m.AddDNSEntries } |
| 122 | +func modelGetHostname(m model) types.String { return m.Hostname } |
| 123 | +func modelGetId(m model) types.String { return m.Id } |
| 124 | +func modelGetIPAddress(m model) types.String { return m.IPAddress } |
| 125 | +func modelGetMACAddress(m model) types.String { return m.MACAddress } |
| 126 | + |
| 127 | +func modelSetAddDNSEntries(m *model, value types.Bool) { m.AddDNSEntries = value } |
| 128 | +func modelSetHostname(m *model, value types.String) { m.Hostname = value } |
| 129 | +func modelSetId(m *model, value types.String) { m.Id = value } |
| 130 | +func modelSetIPAddress(m *model, value types.String) { m.IPAddress = value } |
| 131 | +func modelSetMACAddress(m *model, value types.String) { m.MACAddress = value } |
0 commit comments