|
| 1 | +package domain |
| 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 | + hostnameAttribute = "name" |
| 17 | + hostnameAttributeDescription = "Hostname to assign." |
| 18 | + hostnameUCIOption = "name" |
| 19 | + |
| 20 | + ipAddressAttribute = "ip" |
| 21 | + ipAddressAttributeDescription = "The IP address to be used for this domain." |
| 22 | + ipAddressUCIOption = "ip" |
| 23 | + |
| 24 | + schemaDescription = "Binds a domain name to an IP address." |
| 25 | + |
| 26 | + uciConfig = "dhcp" |
| 27 | + uciType = "domain" |
| 28 | +) |
| 29 | + |
| 30 | +var ( |
| 31 | + hostnameSchemaAttribute = lucirpcglue.StringSchemaAttribute[model, lucirpc.Options, lucirpc.Options]{ |
| 32 | + Description: hostnameAttributeDescription, |
| 33 | + ReadResponse: lucirpcglue.ReadResponseOptionString(modelSetHostname, hostnameAttribute, hostnameUCIOption), |
| 34 | + ResourceExistence: lucirpcglue.Required, |
| 35 | + UpsertRequest: lucirpcglue.UpsertRequestOptionString(modelGetHostname, hostnameAttribute, hostnameUCIOption), |
| 36 | + } |
| 37 | + |
| 38 | + ipAddressSchemaAttribute = lucirpcglue.StringSchemaAttribute[model, lucirpc.Options, lucirpc.Options]{ |
| 39 | + Description: ipAddressAttributeDescription, |
| 40 | + ReadResponse: lucirpcglue.ReadResponseOptionString(modelSetIPAddress, ipAddressAttribute, ipAddressUCIOption), |
| 41 | + ResourceExistence: lucirpcglue.Required, |
| 42 | + UpsertRequest: lucirpcglue.UpsertRequestOptionString(modelGetIPAddress, ipAddressAttribute, ipAddressUCIOption), |
| 43 | + Validators: []validator.String{ |
| 44 | + stringvalidator.Any( |
| 45 | + stringvalidator.RegexMatches( |
| 46 | + regexp.MustCompile("^([[:digit:]]{1,3}.){3}[[:digit:]]{1,3}$"), |
| 47 | + `must be a valid IP address (e.g. "192.168.3.1")`, |
| 48 | + ), |
| 49 | + ), |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + schemaAttributes = map[string]lucirpcglue.SchemaAttribute[model, lucirpc.Options, lucirpc.Options]{ |
| 54 | + hostnameAttribute: hostnameSchemaAttribute, |
| 55 | + ipAddressAttribute: ipAddressSchemaAttribute, |
| 56 | + lucirpcglue.IdAttribute: lucirpcglue.IdSchemaAttribute(modelGetId, modelSetId), |
| 57 | + } |
| 58 | +) |
| 59 | + |
| 60 | +func NewDataSource() datasource.DataSource { |
| 61 | + return lucirpcglue.NewDataSource( |
| 62 | + modelGetId, |
| 63 | + schemaAttributes, |
| 64 | + schemaDescription, |
| 65 | + uciConfig, |
| 66 | + uciType, |
| 67 | + ) |
| 68 | +} |
| 69 | + |
| 70 | +func NewResource() resource.Resource { |
| 71 | + return lucirpcglue.NewResource( |
| 72 | + modelGetId, |
| 73 | + schemaAttributes, |
| 74 | + schemaDescription, |
| 75 | + uciConfig, |
| 76 | + uciType, |
| 77 | + ) |
| 78 | +} |
| 79 | + |
| 80 | +type model struct { |
| 81 | + Hostname types.String `tfsdk:"name"` |
| 82 | + Id types.String `tfsdk:"id"` |
| 83 | + IPAddress types.String `tfsdk:"ip"` |
| 84 | +} |
| 85 | + |
| 86 | +func modelGetHostname(m model) types.String { return m.Hostname } |
| 87 | +func modelGetId(m model) types.String { return m.Id } |
| 88 | +func modelGetIPAddress(m model) types.String { return m.IPAddress } |
| 89 | + |
| 90 | +func modelSetHostname(m *model, value types.String) { m.Hostname = value } |
| 91 | +func modelSetId(m *model, value types.String) { m.Id = value } |
| 92 | +func modelSetIPAddress(m *model, value types.String) { m.IPAddress = value } |
0 commit comments