|
| 1 | +package ovh |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-framework-validators/int64validator" |
| 7 | + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 13 | + ovhtypes "github.com/ovh/terraform-provider-ovh/ovh/types" |
| 14 | + |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 16 | +) |
| 17 | + |
| 18 | +func OkmsServiceKeyResourceSchema(ctx context.Context) schema.Schema { |
| 19 | + attrs := map[string]schema.Attribute{ |
| 20 | + "context": schema.StringAttribute{ |
| 21 | + CustomType: ovhtypes.TfStringType{}, |
| 22 | + Optional: true, |
| 23 | + Computed: true, |
| 24 | + Description: "Context of the key", |
| 25 | + MarkdownDescription: "Context of the key", |
| 26 | + PlanModifiers: []planmodifier.String{ |
| 27 | + stringplanmodifier.RequiresReplaceIfConfigured(), |
| 28 | + stringplanmodifier.UseStateForUnknown(), |
| 29 | + }, |
| 30 | + }, |
| 31 | + "created_at": schema.StringAttribute{ |
| 32 | + CustomType: ovhtypes.TfStringType{}, |
| 33 | + Computed: true, |
| 34 | + Description: "Creation time of the key", |
| 35 | + MarkdownDescription: "Creation time of the key", |
| 36 | + PlanModifiers: []planmodifier.String{ |
| 37 | + stringplanmodifier.UseStateForUnknown(), |
| 38 | + }, |
| 39 | + }, |
| 40 | + "curve": schema.StringAttribute{ |
| 41 | + CustomType: ovhtypes.TfStringType{}, |
| 42 | + Optional: true, |
| 43 | + Computed: true, |
| 44 | + Description: "Curve type for Elliptic Curve (EC) keys", |
| 45 | + MarkdownDescription: "Curve type for Elliptic Curve (EC) keys", |
| 46 | + PlanModifiers: []planmodifier.String{ |
| 47 | + stringplanmodifier.RequiresReplaceIfConfigured(), |
| 48 | + stringplanmodifier.UseStateForUnknown(), |
| 49 | + }, |
| 50 | + Validators: []validator.String{ |
| 51 | + stringvalidator.OneOf( |
| 52 | + "P-256", |
| 53 | + "P-384", |
| 54 | + "P-521", |
| 55 | + ), |
| 56 | + }, |
| 57 | + }, |
| 58 | + "deactivation_reason": schema.StringAttribute{ |
| 59 | + CustomType: ovhtypes.TfStringType{}, |
| 60 | + Computed: true, |
| 61 | + Description: "Key deactivation reason", |
| 62 | + MarkdownDescription: "Key deactivation reason", |
| 63 | + Validators: []validator.String{ |
| 64 | + stringvalidator.OneOf( |
| 65 | + "AFFILIATION_CHANGED", |
| 66 | + "CA_COMPROMISE", |
| 67 | + "CESSATION_OF_OPERATION", |
| 68 | + "KEY_COMPROMISE", |
| 69 | + "PRIVILEGE_WITHDRAWN", |
| 70 | + "SUPERSEDED", |
| 71 | + "UNSPECIFIED", |
| 72 | + ), |
| 73 | + }, |
| 74 | + }, |
| 75 | + "id": schema.StringAttribute{ |
| 76 | + CustomType: ovhtypes.TfStringType{}, |
| 77 | + Computed: true, |
| 78 | + Description: "Key ID", |
| 79 | + MarkdownDescription: "Key ID", |
| 80 | + PlanModifiers: []planmodifier.String{ |
| 81 | + stringplanmodifier.UseStateForUnknown(), |
| 82 | + }, |
| 83 | + }, |
| 84 | + "name": schema.StringAttribute{ |
| 85 | + CustomType: ovhtypes.TfStringType{}, |
| 86 | + Required: true, |
| 87 | + Description: "Key name", |
| 88 | + MarkdownDescription: "Key name", |
| 89 | + }, |
| 90 | + "okms_id": schema.StringAttribute{ |
| 91 | + CustomType: ovhtypes.TfStringType{}, |
| 92 | + Required: true, |
| 93 | + Description: "Okms ID", |
| 94 | + MarkdownDescription: "Okms ID", |
| 95 | + PlanModifiers: []planmodifier.String{ |
| 96 | + stringplanmodifier.RequiresReplace(), |
| 97 | + stringplanmodifier.UseStateForUnknown(), |
| 98 | + }, |
| 99 | + }, |
| 100 | + "operations": schema.ListAttribute{ |
| 101 | + CustomType: ovhtypes.NewTfListNestedType[ovhtypes.TfStringValue](ctx), |
| 102 | + Required: true, |
| 103 | + Description: "The operations for which the key is intended to be used", |
| 104 | + MarkdownDescription: "The operations for which the key is intended to be used", |
| 105 | + PlanModifiers: []planmodifier.List{ |
| 106 | + listplanmodifier.RequiresReplace(), |
| 107 | + listplanmodifier.UseStateForUnknown(), |
| 108 | + }, |
| 109 | + }, |
| 110 | + "size": schema.Int64Attribute{ |
| 111 | + CustomType: ovhtypes.TfInt64Type{}, |
| 112 | + Optional: true, |
| 113 | + Computed: true, |
| 114 | + Description: "Size of the key to be created", |
| 115 | + MarkdownDescription: "Size of the key to be created", |
| 116 | + PlanModifiers: []planmodifier.Int64{ |
| 117 | + int64planmodifier.RequiresReplaceIfConfigured(), |
| 118 | + int64planmodifier.UseStateForUnknown(), |
| 119 | + }, |
| 120 | + Validators: []validator.Int64{ |
| 121 | + int64validator.OneOf( |
| 122 | + 128, |
| 123 | + 192, |
| 124 | + 256, |
| 125 | + 2048, |
| 126 | + 3072, |
| 127 | + 4096, |
| 128 | + ), |
| 129 | + }, |
| 130 | + }, |
| 131 | + "state": schema.StringAttribute{ |
| 132 | + CustomType: ovhtypes.TfStringType{}, |
| 133 | + Computed: true, |
| 134 | + Description: "State of the key", |
| 135 | + MarkdownDescription: "State of the key", |
| 136 | + Validators: []validator.String{ |
| 137 | + stringvalidator.OneOf( |
| 138 | + "ACTIVE", |
| 139 | + "COMPROMISED", |
| 140 | + "DEACTIVATED", |
| 141 | + ), |
| 142 | + }, |
| 143 | + }, |
| 144 | + "type": schema.StringAttribute{ |
| 145 | + CustomType: ovhtypes.TfStringType{}, |
| 146 | + Required: true, |
| 147 | + Description: "Type of the key to be created", |
| 148 | + MarkdownDescription: "Type of the key to be created", |
| 149 | + PlanModifiers: []planmodifier.String{ |
| 150 | + stringplanmodifier.RequiresReplace(), |
| 151 | + stringplanmodifier.UseStateForUnknown(), |
| 152 | + }, |
| 153 | + Validators: []validator.String{ |
| 154 | + stringvalidator.OneOf( |
| 155 | + "EC", |
| 156 | + "RSA", |
| 157 | + "oct", |
| 158 | + ), |
| 159 | + }, |
| 160 | + }, |
| 161 | + } |
| 162 | + |
| 163 | + return schema.Schema{ |
| 164 | + Attributes: attrs, |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +type OkmsServiceKeyResourceModel struct { |
| 169 | + Context ovhtypes.TfStringValue `tfsdk:"context" json:"context"` |
| 170 | + CreatedAt ovhtypes.TfStringValue `tfsdk:"created_at" json:"createdAt"` |
| 171 | + Curve ovhtypes.TfStringValue `tfsdk:"curve" json:"curve"` |
| 172 | + DeactivationReason ovhtypes.TfStringValue `tfsdk:"deactivation_reason" json:"deactivationReason"` |
| 173 | + Id ovhtypes.TfStringValue `tfsdk:"id" json:"id"` |
| 174 | + Name ovhtypes.TfStringValue `tfsdk:"name" json:"name"` |
| 175 | + OkmsId ovhtypes.TfStringValue `tfsdk:"okms_id" json:"okmsId"` |
| 176 | + Operations ovhtypes.TfListNestedValue[ovhtypes.TfStringValue] `tfsdk:"operations" json:"operations"` |
| 177 | + Size ovhtypes.TfInt64Value `tfsdk:"size" json:"size"` |
| 178 | + State ovhtypes.TfStringValue `tfsdk:"state" json:"state"` |
| 179 | + Type ovhtypes.TfStringValue `tfsdk:"type" json:"type"` |
| 180 | +} |
| 181 | + |
| 182 | +func (v *OkmsServiceKeyResourceModel) MergeWith(other *OkmsServiceKeyResourceModel) { |
| 183 | + |
| 184 | + if (v.Context.IsUnknown() || v.Context.IsNull()) && !other.Context.IsUnknown() { |
| 185 | + v.Context = other.Context |
| 186 | + } |
| 187 | + |
| 188 | + if (v.CreatedAt.IsUnknown() || v.CreatedAt.IsNull()) && !other.CreatedAt.IsUnknown() { |
| 189 | + v.CreatedAt = other.CreatedAt |
| 190 | + } |
| 191 | + |
| 192 | + if (v.Curve.IsUnknown() || v.Curve.IsNull()) && !other.Curve.IsUnknown() { |
| 193 | + v.Curve = other.Curve |
| 194 | + } |
| 195 | + |
| 196 | + if (v.DeactivationReason.IsUnknown() || v.DeactivationReason.IsNull()) && !other.DeactivationReason.IsUnknown() { |
| 197 | + v.DeactivationReason = other.DeactivationReason |
| 198 | + } |
| 199 | + |
| 200 | + if (v.Id.IsUnknown() || v.Id.IsNull()) && !other.Id.IsUnknown() { |
| 201 | + v.Id = other.Id |
| 202 | + } |
| 203 | + |
| 204 | + if (v.Name.IsUnknown() || v.Name.IsNull()) && !other.Name.IsUnknown() { |
| 205 | + v.Name = other.Name |
| 206 | + } |
| 207 | + |
| 208 | + if (v.OkmsId.IsUnknown() || v.OkmsId.IsNull()) && !other.OkmsId.IsUnknown() { |
| 209 | + v.OkmsId = other.OkmsId |
| 210 | + } |
| 211 | + |
| 212 | + if (v.Operations.IsUnknown() || v.Operations.IsNull()) && !other.Operations.IsUnknown() { |
| 213 | + v.Operations = other.Operations |
| 214 | + } |
| 215 | + |
| 216 | + if (v.Size.IsUnknown() || v.Size.IsNull()) && !other.Size.IsUnknown() { |
| 217 | + v.Size = other.Size |
| 218 | + } |
| 219 | + |
| 220 | + if (v.State.IsUnknown() || v.State.IsNull()) && !other.State.IsUnknown() { |
| 221 | + v.State = other.State |
| 222 | + } |
| 223 | + |
| 224 | + if (v.Type.IsUnknown() || v.Type.IsNull()) && !other.Type.IsUnknown() { |
| 225 | + v.Type = other.Type |
| 226 | + } |
| 227 | +} |
| 228 | + |
| 229 | +type OkmsServiceKeyWritableModel struct { |
| 230 | + Context *ovhtypes.TfStringValue `tfsdk:"context" json:"context,omitempty"` |
| 231 | + Curve *ovhtypes.TfStringValue `tfsdk:"curve" json:"curve,omitempty"` |
| 232 | + DeactivationReason *ovhtypes.TfStringValue `tfsdk:"deactivation_reason" json:"deactivationReason,omitempty"` |
| 233 | + Name *ovhtypes.TfStringValue `tfsdk:"name" json:"name,omitempty"` |
| 234 | + Operations *ovhtypes.TfListNestedValue[ovhtypes.TfStringValue] `tfsdk:"operations" json:"operations,omitempty"` |
| 235 | + Size *ovhtypes.TfInt64Value `tfsdk:"size" json:"size,omitempty"` |
| 236 | + State *ovhtypes.TfStringValue `tfsdk:"state" json:"state,omitempty"` |
| 237 | + Type *ovhtypes.TfStringValue `tfsdk:"type" json:"type,omitempty"` |
| 238 | +} |
| 239 | + |
| 240 | +func (v OkmsServiceKeyResourceModel) ToCreate() *OkmsServiceKeyWritableModel { |
| 241 | + res := &OkmsServiceKeyWritableModel{} |
| 242 | + |
| 243 | + if !v.Context.IsUnknown() { |
| 244 | + res.Context = &v.Context |
| 245 | + } |
| 246 | + |
| 247 | + if !v.Curve.IsUnknown() { |
| 248 | + res.Curve = &v.Curve |
| 249 | + } |
| 250 | + |
| 251 | + if !v.Name.IsUnknown() { |
| 252 | + res.Name = &v.Name |
| 253 | + } |
| 254 | + |
| 255 | + if !v.Operations.IsUnknown() { |
| 256 | + res.Operations = &v.Operations |
| 257 | + } |
| 258 | + |
| 259 | + if !v.Size.IsUnknown() { |
| 260 | + res.Size = &v.Size |
| 261 | + } |
| 262 | + |
| 263 | + if !v.Type.IsUnknown() { |
| 264 | + res.Type = &v.Type |
| 265 | + } |
| 266 | + |
| 267 | + return res |
| 268 | +} |
| 269 | + |
| 270 | +func (v OkmsServiceKeyResourceModel) ToUpdate() *OkmsServiceKeyWritableModel { |
| 271 | + res := &OkmsServiceKeyWritableModel{} |
| 272 | + |
| 273 | + if !v.DeactivationReason.IsUnknown() { |
| 274 | + res.DeactivationReason = &v.DeactivationReason |
| 275 | + } |
| 276 | + |
| 277 | + if !v.Name.IsUnknown() { |
| 278 | + res.Name = &v.Name |
| 279 | + } |
| 280 | + |
| 281 | + if !v.State.IsUnknown() { |
| 282 | + res.State = &v.State |
| 283 | + } |
| 284 | + |
| 285 | + return res |
| 286 | +} |
0 commit comments