|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/coder/coder/v2/codersdk" |
| 8 | + "github.com/google/uuid" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/attr/xattr" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" |
| 13 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 14 | +) |
| 15 | + |
| 16 | +type groupSyncSettingsType struct { |
| 17 | + basetypes.MapType |
| 18 | +} |
| 19 | + |
| 20 | +var _ basetypes.MapTypable = GroupSyncSettingsType |
| 21 | + |
| 22 | +var GroupSyncSettingsType = groupSyncSettingsType{} |
| 23 | + |
| 24 | +func (t groupSyncSettingsType) ValueType(ctx context.Context) attr.Value { |
| 25 | + return GroupSyncSettings{} |
| 26 | +} |
| 27 | + |
| 28 | +// Equal implements basetypes.StringTypable. |
| 29 | +func (t groupSyncSettingsType) Equal(o attr.Type) bool { |
| 30 | + if o, ok := o.(groupSyncSettingsType); ok { |
| 31 | + return t.MapType.Equal(o.MapType) |
| 32 | + } |
| 33 | + return false |
| 34 | +} |
| 35 | + |
| 36 | +// ValueFromString implements basetypes.StringTypable. |
| 37 | +func (t groupSyncSettingsType) ValueFromString(ctx context.Context, in basetypes.StringValue) (basetypes.StringValuable, diag.Diagnostics) { |
| 38 | + var diags diag.Diagnostics |
| 39 | + |
| 40 | + if in.IsNull() { |
| 41 | + return NewUUIDNull(), diags |
| 42 | + } |
| 43 | + if in.IsUnknown() { |
| 44 | + return NewUUIDUnknown(), diags |
| 45 | + } |
| 46 | + |
| 47 | + value, err := uuid.Parse(in.ValueString()) |
| 48 | + if err != nil { |
| 49 | + // The framework doesn't want us to return validation errors here |
| 50 | + // for some reason. They get caught by `ValidateAttribute` instead, |
| 51 | + // and this function isn't called directly by our provider - UUIDValue |
| 52 | + // takes a valid GroupSyncSettings instead of a string. |
| 53 | + return NewUUIDUnknown(), diags |
| 54 | + } |
| 55 | + |
| 56 | + return UUIDValue(value), diags |
| 57 | +} |
| 58 | + |
| 59 | +// ValueFromTerraform implements basetypes.StringTypable. |
| 60 | +func (t groupSyncSettingsType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) { |
| 61 | + attrValue, err := t.StringType.ValueFromTerraform(ctx, in) |
| 62 | + |
| 63 | + if err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + |
| 67 | + stringValue, ok := attrValue.(basetypes.StringValue) |
| 68 | + |
| 69 | + if !ok { |
| 70 | + return nil, fmt.Errorf("unexpected type %T, expected basetypes.StringValue", attrValue) |
| 71 | + } |
| 72 | + |
| 73 | + stringValuable, diags := t.ValueFromString(ctx, stringValue) |
| 74 | + |
| 75 | + if diags.HasError() { |
| 76 | + return nil, fmt.Errorf("unexpected error converting StringValue to StringValuable: %v", diags) |
| 77 | + } |
| 78 | + |
| 79 | + return stringValuable, nil |
| 80 | +} |
| 81 | + |
| 82 | +type GroupSyncSettings struct { |
| 83 | + // The framework requires custom types extend a primitive or object. |
| 84 | + basetypes.MapValue |
| 85 | + value codersdk.GroupSyncSettings |
| 86 | +} |
| 87 | + |
| 88 | +var ( |
| 89 | + _ basetypes.MapValuable = GroupSyncSettings{} |
| 90 | + _ xattr.ValidateableAttribute = GroupSyncSettings{} |
| 91 | +) |
| 92 | + |
| 93 | +func NewGroupSyncSettingsNull() GroupSyncSettings { |
| 94 | + return GroupSyncSettings{ |
| 95 | + MapValue: basetypes.NewMapNull(), |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func NewGroupSyncSettingsUnknown() GroupSyncSettings { |
| 100 | + return GroupSyncSettings{ |
| 101 | + MapValue: basetypes.NewMapUnknown(), |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func GroupSyncSettingsValue(value uuid.UUID) UUID { |
| 106 | + return UUID{ |
| 107 | + MapValue: basetypes.NewStringValue(value.String()), |
| 108 | + value: value, |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// Equal implements basetypes.StringValuable. |
| 113 | +func (v GroupSyncSettings) Equal(o attr.Value) bool { |
| 114 | + if o, ok := o.(GroupSyncSettings); ok { |
| 115 | + return v.StringValue.Equal(o.StringValue) |
| 116 | + } |
| 117 | + return false |
| 118 | +} |
| 119 | + |
| 120 | +// Type implements basetypes.StringValuable. |
| 121 | +func (v GroupSyncSettings) Type(context.Context) attr.Type { |
| 122 | + return GroupSyncSettingsType |
| 123 | +} |
| 124 | + |
| 125 | +// ValueUUID returns the GroupSyncSettings value. If the value is null or unknown, returns the Nil GroupSyncSettings. |
| 126 | +func (v GroupSyncSettings) ValueUUID() uuid.GroupSyncSettings { |
| 127 | + return v.value |
| 128 | +} |
| 129 | + |
| 130 | +// ValidateAttribute implements xattr.ValidateableAttribute. |
| 131 | +func (v GroupSyncSettings) ValidateAttribute(ctx context.Context, req xattr.ValidateAttributeRequest, resp *xattr.ValidateAttributeResponse) { |
| 132 | + if v.IsNull() || v.IsUnknown() { |
| 133 | + return |
| 134 | + } |
| 135 | + |
| 136 | + if _, err := uuid.Parse(v.ValueString()); err != nil { |
| 137 | + resp.Diagnostics.AddAttributeError( |
| 138 | + req.Path, |
| 139 | + "Invalid GroupSyncSettings", |
| 140 | + "The provided value cannot be parsed as a GroupSyncSettings\n\n"+ |
| 141 | + "Path: "+req.Path.String()+"\n"+ |
| 142 | + "Error: "+err.Error(), |
| 143 | + ) |
| 144 | + } |
| 145 | +} |
0 commit comments