|
| 1 | +// Copyright (c) ZStack.io, Inc. |
| 2 | + |
| 3 | +package provider |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 14 | + "zstack.io/zstack-sdk-go/pkg/client" |
| 15 | +) |
| 16 | + |
| 17 | +var ( |
| 18 | + _ resource.Resource = &tagAttachmentResource{} |
| 19 | + _ resource.ResourceWithConfigure = &tagAttachmentResource{} |
| 20 | +) |
| 21 | + |
| 22 | +type tagAttachmentResource struct { |
| 23 | + client *client.ZSClient |
| 24 | +} |
| 25 | + |
| 26 | +type tagAttachmentModel struct { |
| 27 | + //ID types.String `tfsdk:"id"` |
| 28 | + TagUuid types.String `tfsdk:"tag_uuid"` |
| 29 | + ResourceUuids types.List `tfsdk:"resource_uuids"` |
| 30 | + Tokens types.Map `tfsdk:"tokens"` |
| 31 | +} |
| 32 | + |
| 33 | +func TagAttachmentResource() resource.Resource { |
| 34 | + return &tagAttachmentResource{} |
| 35 | +} |
| 36 | + |
| 37 | +func (r *tagAttachmentResource) Configure(ctx context.Context, request resource.ConfigureRequest, response *resource.ConfigureResponse) { |
| 38 | + if request.ProviderData == nil { |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + client, ok := request.ProviderData.(*client.ZSClient) |
| 43 | + if !ok { |
| 44 | + response.Diagnostics.AddError( |
| 45 | + "Unexpected Data Source Configure Type", |
| 46 | + fmt.Sprintf("Expected *client.ZSClient, got: %T. Please report this issue to the Provider developer. ", request.ProviderData), |
| 47 | + ) |
| 48 | + |
| 49 | + return |
| 50 | + } |
| 51 | + r.client = client |
| 52 | +} |
| 53 | + |
| 54 | +func (r *tagAttachmentResource) Metadata(_ context.Context, request resource.MetadataRequest, response *resource.MetadataResponse) { |
| 55 | + response.TypeName = request.ProviderTypeName + "_tag_attachment" |
| 56 | +} |
| 57 | + |
| 58 | +func (r *tagAttachmentResource) Schema(_ context.Context, request resource.SchemaRequest, response *resource.SchemaResponse) { |
| 59 | + response.Schema = schema.Schema{ |
| 60 | + Description: "ZStack Tag Attach to Resource", |
| 61 | + Attributes: map[string]schema.Attribute{ |
| 62 | + "tag_uuid": schema.StringAttribute{ |
| 63 | + Required: true, |
| 64 | + Description: "The UUID of the tag.", |
| 65 | + }, |
| 66 | + "resource_uuids": schema.ListAttribute{ |
| 67 | + Required: true, |
| 68 | + ElementType: types.StringType, |
| 69 | + Description: "The uuid of the resource.", |
| 70 | + }, |
| 71 | + "tokens": schema.MapAttribute{ |
| 72 | + Optional: true, |
| 73 | + ElementType: types.StringType, |
| 74 | + Description: "If attach type is 'withToken', you must set this. For simple attach, leave it empty or omit.", |
| 75 | + }, |
| 76 | + }, |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func (r *tagAttachmentResource) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) { |
| 81 | + var plan tagAttachmentModel |
| 82 | + diags := request.Plan.Get(ctx, &plan) |
| 83 | + response.Diagnostics.Append(diags...) |
| 84 | + if response.Diagnostics.HasError() { |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + if r.client == nil { |
| 89 | + response.Diagnostics.AddWarning("Client Not Configured", "The client was not properly configured.") |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + resourceUuids := make([]string, 0, len(plan.ResourceUuids.Elements())) |
| 94 | + for _, v := range plan.ResourceUuids.Elements() { |
| 95 | + resourceUuids = append(resourceUuids, v.(types.String).ValueString()) |
| 96 | + } |
| 97 | + |
| 98 | + attachType := []string{} |
| 99 | + if !plan.Tokens.IsNull() && len(plan.Tokens.Elements()) > 0 { |
| 100 | + tokenMap := make(map[string]interface{}) |
| 101 | + for k, v := range plan.Tokens.Elements() { |
| 102 | + tokenMap[k] = v.(types.String).ValueString() |
| 103 | + } |
| 104 | + tokenJson, err := json.Marshal(tokenMap) |
| 105 | + if err != nil { |
| 106 | + response.Diagnostics.AddError("Error marshaling tokens", err.Error()) |
| 107 | + return |
| 108 | + } |
| 109 | + attachType = []string{"withToken", string(tokenJson)} |
| 110 | + } |
| 111 | + |
| 112 | + _, err := r.client.AttachTagToResource(plan.TagUuid.ValueString(), resourceUuids, attachType...) |
| 113 | + if err != nil { |
| 114 | + response.Diagnostics.AddError("Error attaching tag", err.Error()) |
| 115 | + return |
| 116 | + } |
| 117 | + |
| 118 | + //id := fmt.Sprintf("%s|%s", plan.TagUuid.ValueString(), strings.Join(resourceUuids, ",")) |
| 119 | + response.State.Set(ctx, &tagAttachmentModel{ |
| 120 | + TagUuid: plan.TagUuid, |
| 121 | + ResourceUuids: plan.ResourceUuids, |
| 122 | + Tokens: plan.Tokens, |
| 123 | + }) |
| 124 | + |
| 125 | +} |
| 126 | + |
| 127 | +func (r *tagAttachmentResource) Read(ctx context.Context, request resource.ReadRequest, response *resource.ReadResponse) { |
| 128 | + |
| 129 | + var state tagAttachmentModel |
| 130 | + diags := request.State.Get(ctx, &state) |
| 131 | + response.Diagnostics.Append(diags...) |
| 132 | + if response.Diagnostics.HasError() { |
| 133 | + return |
| 134 | + } |
| 135 | + |
| 136 | + if r.client == nil { |
| 137 | + response.Diagnostics.AddWarning("Client Not Configured", "The client was not properly configured.") |
| 138 | + return |
| 139 | + } |
| 140 | + |
| 141 | + tagUuid := state.TagUuid.ValueString() |
| 142 | + if tagUuid == "" { |
| 143 | + response.Diagnostics.AddError("Tag UUID is empty", "Cannot read tag attachment without a valid tag UUID.") |
| 144 | + return |
| 145 | + } |
| 146 | + result, err := r.client.GetUserTag(tagUuid) |
| 147 | + if err != nil { |
| 148 | + response.Diagnostics.AddError( |
| 149 | + "Unable to Read Tag Binding", |
| 150 | + fmt.Sprintf("Error retrieving tag binding for tag UUID %s: %s", tagUuid, err), |
| 151 | + ) |
| 152 | + return |
| 153 | + } |
| 154 | + if len(result) == 0 { |
| 155 | + response.State.RemoveResource(ctx) |
| 156 | + return |
| 157 | + } |
| 158 | + |
| 159 | + var resourceUuids []string |
| 160 | + for _, item := range result { |
| 161 | + resourceUuids = append(resourceUuids, item.ResourceUuid) |
| 162 | + } |
| 163 | + |
| 164 | + // Update state.ResourceUuids |
| 165 | + var resourceUuidsValues []attr.Value |
| 166 | + for _, uuid := range resourceUuids { |
| 167 | + resourceUuidsValues = append(resourceUuidsValues, types.StringValue(uuid)) |
| 168 | + } |
| 169 | + |
| 170 | + state.ResourceUuids = types.ListValueMust(types.StringType, resourceUuidsValues) |
| 171 | + state.TagUuid = types.StringValue(tagUuid) |
| 172 | + |
| 173 | + diags = response.State.Set(ctx, &state) |
| 174 | + response.Diagnostics.Append(diags...) |
| 175 | + |
| 176 | +} |
| 177 | + |
| 178 | +func (r *tagAttachmentResource) Update(ctx context.Context, request resource.UpdateRequest, response *resource.UpdateResponse) { |
| 179 | + response.Diagnostics.AddWarning("Update not supported", "Please recreate the resource if you need changes") |
| 180 | +} |
| 181 | + |
| 182 | +func (r *tagAttachmentResource) Delete(ctx context.Context, request resource.DeleteRequest, response *resource.DeleteResponse) { |
| 183 | + var state tagAttachmentModel |
| 184 | + diags := request.State.Get(ctx, &state) |
| 185 | + response.Diagnostics.Append(diags...) |
| 186 | + if response.Diagnostics.HasError() { |
| 187 | + return |
| 188 | + } |
| 189 | + |
| 190 | + resourceUuids := make([]string, 0, len(state.ResourceUuids.Elements())) |
| 191 | + for _, v := range state.ResourceUuids.Elements() { |
| 192 | + resourceUuids = append(resourceUuids, v.(types.String).ValueString()) |
| 193 | + } |
| 194 | + |
| 195 | + err := r.client.DetachTagFromResource(state.TagUuid.ValueString(), resourceUuids) |
| 196 | + if err != nil { |
| 197 | + response.Diagnostics.AddError("Error detaching tag", err.Error()) |
| 198 | + return |
| 199 | + } |
| 200 | + |
| 201 | +} |
0 commit comments