|
| 1 | +package odhcpd |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/hashicorp/terraform-plugin-framework-validators/int64validator" |
| 5 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 6 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 9 | + "github.com/joneshf/terraform-provider-openwrt/lucirpc" |
| 10 | + "github.com/joneshf/terraform-provider-openwrt/openwrt/internal/lucirpcglue" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + leaseFileAttribute = "leasefile" |
| 15 | + leaseFileAttributeDescription = "Location of the lease/hostfile for DHCPv4 and DHCPv6." |
| 16 | + leaseFileUCIOption = "leasefile" |
| 17 | + |
| 18 | + leaseTriggerAttribute = "leasetrigger" |
| 19 | + leaseTriggerAttributeDescription = "Location of the lease trigger script." |
| 20 | + leaseTriggerUCIOption = "leasetrigger" |
| 21 | + |
| 22 | + legacyAttribute = "legacy" |
| 23 | + legacyAttributeDescription = "Enable DHCPv4 if the 'dhcp' section constains a `start` option, but no `dhcpv4` option set." |
| 24 | + legacyUCIOption = "legacy" |
| 25 | + |
| 26 | + logLevelAttribute = "loglevel" |
| 27 | + logLevelAttributeDescription = "Syslog level priority (0-7)." |
| 28 | + logLevelUCIOption = "loglevel" |
| 29 | + |
| 30 | + mainDHCPAttribute = "maindhcp" |
| 31 | + mainDHCPAttributeDescription = "Use odhcpd as the main DHCPv4 service." |
| 32 | + mainDHCPUCIOption = "maindhcp" |
| 33 | + |
| 34 | + schemaDescription = "An embedded DHCP/DHCPv6/RA server & NDP relay." |
| 35 | + |
| 36 | + uciConfig = "dhcp" |
| 37 | + uciType = "odhcpd" |
| 38 | +) |
| 39 | + |
| 40 | +var ( |
| 41 | + leaseFileSchemaAttribute = lucirpcglue.StringSchemaAttribute[model, lucirpc.Options, lucirpc.Options]{ |
| 42 | + Description: leaseFileAttributeDescription, |
| 43 | + ReadResponse: lucirpcglue.ReadResponseOptionString(modelSetLeaseFile, leaseFileAttribute, leaseFileUCIOption), |
| 44 | + ResourceExistence: lucirpcglue.NoValidation, |
| 45 | + UpsertRequest: lucirpcglue.UpsertRequestOptionString(modelGetLeaseFile, leaseFileAttribute, leaseFileUCIOption), |
| 46 | + } |
| 47 | + |
| 48 | + leaseTriggerSchemaAttribute = lucirpcglue.StringSchemaAttribute[model, lucirpc.Options, lucirpc.Options]{ |
| 49 | + Description: leaseTriggerAttributeDescription, |
| 50 | + ReadResponse: lucirpcglue.ReadResponseOptionString(modelSetLeaseTrigger, leaseTriggerAttribute, leaseTriggerUCIOption), |
| 51 | + ResourceExistence: lucirpcglue.NoValidation, |
| 52 | + UpsertRequest: lucirpcglue.UpsertRequestOptionString(modelGetLeaseTrigger, leaseTriggerAttribute, leaseTriggerUCIOption), |
| 53 | + } |
| 54 | + |
| 55 | + legacySchemaAttribute = lucirpcglue.BoolSchemaAttribute[model, lucirpc.Options, lucirpc.Options]{ |
| 56 | + Description: legacyAttributeDescription, |
| 57 | + ReadResponse: lucirpcglue.ReadResponseOptionBool(modelSetLegacy, legacyAttribute, legacyUCIOption), |
| 58 | + ResourceExistence: lucirpcglue.NoValidation, |
| 59 | + UpsertRequest: lucirpcglue.UpsertRequestOptionBool(modelGetLegacy, legacyAttribute, legacyUCIOption), |
| 60 | + } |
| 61 | + |
| 62 | + logLevelSchemaAttribute = lucirpcglue.Int64SchemaAttribute[model, lucirpc.Options, lucirpc.Options]{ |
| 63 | + Description: logLevelAttributeDescription, |
| 64 | + ReadResponse: lucirpcglue.ReadResponseOptionInt64(modelSetLogLevel, logLevelAttribute, logLevelUCIOption), |
| 65 | + ResourceExistence: lucirpcglue.NoValidation, |
| 66 | + UpsertRequest: lucirpcglue.UpsertRequestOptionInt64(modelGetLogLevel, logLevelAttribute, logLevelUCIOption), |
| 67 | + Validators: []validator.Int64{ |
| 68 | + int64validator.Between(0, 7), |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + mainDHCPSchemaAttribute = lucirpcglue.BoolSchemaAttribute[model, lucirpc.Options, lucirpc.Options]{ |
| 73 | + Description: mainDHCPAttributeDescription, |
| 74 | + ReadResponse: lucirpcglue.ReadResponseOptionBool(modelSetMainDHCP, mainDHCPAttribute, mainDHCPUCIOption), |
| 75 | + ResourceExistence: lucirpcglue.NoValidation, |
| 76 | + UpsertRequest: lucirpcglue.UpsertRequestOptionBool(modelGetMainDHCP, mainDHCPAttribute, mainDHCPUCIOption), |
| 77 | + } |
| 78 | + |
| 79 | + schemaAttributes = map[string]lucirpcglue.SchemaAttribute[model, lucirpc.Options, lucirpc.Options]{ |
| 80 | + leaseFileAttribute: leaseFileSchemaAttribute, |
| 81 | + leaseTriggerAttribute: leaseTriggerSchemaAttribute, |
| 82 | + legacyAttribute: legacySchemaAttribute, |
| 83 | + logLevelAttribute: logLevelSchemaAttribute, |
| 84 | + lucirpcglue.IdAttribute: lucirpcglue.IdSchemaAttribute(modelGetId, modelSetId), |
| 85 | + mainDHCPAttribute: mainDHCPSchemaAttribute, |
| 86 | + } |
| 87 | +) |
| 88 | + |
| 89 | +func NewDataSource() datasource.DataSource { |
| 90 | + return lucirpcglue.NewDataSource( |
| 91 | + modelGetId, |
| 92 | + schemaAttributes, |
| 93 | + schemaDescription, |
| 94 | + uciConfig, |
| 95 | + uciType, |
| 96 | + ) |
| 97 | +} |
| 98 | + |
| 99 | +func NewResource() resource.Resource { |
| 100 | + return lucirpcglue.NewResource( |
| 101 | + modelGetId, |
| 102 | + schemaAttributes, |
| 103 | + schemaDescription, |
| 104 | + uciConfig, |
| 105 | + uciType, |
| 106 | + ) |
| 107 | +} |
| 108 | + |
| 109 | +type model struct { |
| 110 | + Id types.String `tfsdk:"id"` |
| 111 | + LeaseFile types.String `tfsdk:"leasefile"` |
| 112 | + LeaseTrigger types.String `tfsdk:"leasetrigger"` |
| 113 | + Legacy types.Bool `tfsdk:"legacy"` |
| 114 | + LogLevel types.Int64 `tfsdk:"loglevel"` |
| 115 | + MainDHCP types.Bool `tfsdk:"maindhcp"` |
| 116 | +} |
| 117 | + |
| 118 | +func modelGetId(m model) types.String { return m.Id } |
| 119 | +func modelGetLeaseFile(m model) types.String { return m.LeaseFile } |
| 120 | +func modelGetLeaseTrigger(m model) types.String { return m.LeaseTrigger } |
| 121 | +func modelGetLegacy(m model) types.Bool { return m.Legacy } |
| 122 | +func modelGetLogLevel(m model) types.Int64 { return m.LogLevel } |
| 123 | +func modelGetMainDHCP(m model) types.Bool { return m.MainDHCP } |
| 124 | + |
| 125 | +func modelSetId(m *model, value types.String) { m.Id = value } |
| 126 | +func modelSetLeaseFile(m *model, value types.String) { m.LeaseFile = value } |
| 127 | +func modelSetLeaseTrigger(m *model, value types.String) { m.LeaseTrigger = value } |
| 128 | +func modelSetLegacy(m *model, value types.Bool) { m.Legacy = value } |
| 129 | +func modelSetLogLevel(m *model, value types.Int64) { m.LogLevel = value } |
| 130 | +func modelSetMainDHCP(m *model, value types.Bool) { m.MainDHCP = value } |
0 commit comments