|
| 1 | +package resources |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/int32default" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 18 | + "github.com/qualitorque/terraform-provider-torque/client" |
| 19 | + "github.com/qualitorque/terraform-provider-torque/internal/validators" |
| 20 | +) |
| 21 | + |
| 22 | +// Ensure provider defined types fully satisfy framework interfaces. |
| 23 | +var _ resource.Resource = &TorqueSpaceAdoServerRepositoryResource{} |
| 24 | +var _ resource.ResourceWithImportState = &TorqueSpaceAdoServerRepositoryResource{} |
| 25 | + |
| 26 | +func NewTorqueSpaceAdoServerRepositoryResource() resource.Resource { |
| 27 | + return &TorqueSpaceAdoServerRepositoryResource{} |
| 28 | +} |
| 29 | + |
| 30 | +// TorqueAgentSpaceAssociationResource defines the resource implementation. |
| 31 | +type TorqueSpaceAdoServerRepositoryResource struct { |
| 32 | + client *client.Client |
| 33 | +} |
| 34 | + |
| 35 | +type TorqueSpaceAdoServerRepositoryResourceModel struct { |
| 36 | + SpaceName types.String `tfsdk:"space_name"` |
| 37 | + RepositoryName types.String `tfsdk:"repository_name"` |
| 38 | + RepositoryUrl types.String `tfsdk:"repository_url"` |
| 39 | + Token types.String `tfsdk:"token"` |
| 40 | + Branch types.String `tfsdk:"branch"` |
| 41 | + CredentialName types.String `tfsdk:"credential_name"` |
| 42 | + UseAllAgents types.Bool `tfsdk:"use_all_agents"` |
| 43 | + Agents types.List `tfsdk:"agents"` |
| 44 | + TimeOut types.Int32 `tfsdk:"timeout"` |
| 45 | + AutoRegisterEac types.Bool `tfsdk:"auto_register_eac"` |
| 46 | +} |
| 47 | + |
| 48 | +func (r *TorqueSpaceAdoServerRepositoryResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 49 | + resp.TypeName = "torque_ado_server_repository_space_association" |
| 50 | +} |
| 51 | + |
| 52 | +func (r *TorqueSpaceAdoServerRepositoryResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { |
| 53 | + resp.Schema = schema.Schema{ |
| 54 | + MarkdownDescription: "Onboard a new Ado Server repository into an existing space", |
| 55 | + |
| 56 | + Attributes: map[string]schema.Attribute{ |
| 57 | + "space_name": schema.StringAttribute{ |
| 58 | + MarkdownDescription: "Existing Torque Space name", |
| 59 | + Required: true, |
| 60 | + PlanModifiers: []planmodifier.String{ |
| 61 | + stringplanmodifier.RequiresReplace(), |
| 62 | + }, |
| 63 | + }, |
| 64 | + "repository_name": schema.StringAttribute{ |
| 65 | + Description: "The name of the Ado Server repository to onboard. In this example, repo_name", |
| 66 | + Required: true, |
| 67 | + PlanModifiers: []planmodifier.String{ |
| 68 | + stringplanmodifier.RequiresReplace(), |
| 69 | + }, |
| 70 | + }, |
| 71 | + "repository_url": schema.StringAttribute{ |
| 72 | + Description: "The url of the specific Ado Server repository/project to onboard. For example: https://ado-on-prem.example.com/repo_name", |
| 73 | + Required: true, |
| 74 | + PlanModifiers: []planmodifier.String{ |
| 75 | + stringplanmodifier.RequiresReplace(), |
| 76 | + }, |
| 77 | + }, |
| 78 | + "token": schema.StringAttribute{ |
| 79 | + Description: "Authentication Token to the project/repository. If omitted, existing credentials provided in the credential_name field will be used for authentication. If provided, a new credentials object will be created.", |
| 80 | + Optional: true, |
| 81 | + PlanModifiers: []planmodifier.String{ |
| 82 | + stringplanmodifier.RequiresReplace(), |
| 83 | + }, |
| 84 | + DeprecationMessage: "The token attribute is deprecated and will be removed in a future release. Use the torque_git_credentials resource to store the token and reference it in this resource using the credential_name attribute instead.", |
| 85 | + }, |
| 86 | + "branch": schema.StringAttribute{ |
| 87 | + Description: "Repository branch to use for blueprints and automation assets", |
| 88 | + Required: true, |
| 89 | + PlanModifiers: []planmodifier.String{ |
| 90 | + stringplanmodifier.RequiresReplace(), |
| 91 | + }, |
| 92 | + }, |
| 93 | + "credential_name": schema.StringAttribute{ |
| 94 | + Description: "The name of the Credentials to use/create. Must be unique in the space.", |
| 95 | + Required: true, |
| 96 | + }, |
| 97 | + "use_all_agents": schema.BoolAttribute{ |
| 98 | + Description: "Whether all associated agents can be used to onboard and sync this repository. Must be set to false if agents attribute is used.", |
| 99 | + Default: booldefault.StaticBool(true), |
| 100 | + Optional: true, |
| 101 | + Computed: true, |
| 102 | + Validators: []validator.Bool{validators.UseAllAgentsValidator{}}, |
| 103 | + }, |
| 104 | + "agents": schema.ListAttribute{ |
| 105 | + Description: "List of specific agents to use to onboard and sync this repository. Cannot be specified when use_all_agents is true.", |
| 106 | + Required: false, |
| 107 | + Optional: true, |
| 108 | + ElementType: types.StringType, |
| 109 | + }, |
| 110 | + "timeout": schema.Int32Attribute{ |
| 111 | + Description: "Time in minutes to wait for Torque to sync the repository during the onboarding. Default is 1 minute.", |
| 112 | + Required: false, |
| 113 | + Optional: true, |
| 114 | + Computed: true, |
| 115 | + Default: int32default.StaticInt32(1), |
| 116 | + }, |
| 117 | + "auto_register_eac": schema.BoolAttribute{ |
| 118 | + Description: "Auto register environment files", |
| 119 | + Default: booldefault.StaticBool(false), |
| 120 | + Required: false, |
| 121 | + Optional: true, |
| 122 | + Computed: true, |
| 123 | + }, |
| 124 | + }, |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func (r *TorqueSpaceAdoServerRepositoryResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { |
| 129 | + // Prevent panic if the provider has not been configured. |
| 130 | + if req.ProviderData == nil { |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + client, ok := req.ProviderData.(*client.Client) |
| 135 | + |
| 136 | + if !ok { |
| 137 | + resp.Diagnostics.AddError( |
| 138 | + "Unexpected Resource Configure Type", |
| 139 | + fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 140 | + ) |
| 141 | + |
| 142 | + return |
| 143 | + } |
| 144 | + |
| 145 | + r.client = client |
| 146 | +} |
| 147 | + |
| 148 | +func (r *TorqueSpaceAdoServerRepositoryResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 149 | + var data TorqueSpaceAdoServerRepositoryResourceModel |
| 150 | + const ( |
| 151 | + StatusSyncing = "Syncing" |
| 152 | + StatusConnected = "Connected" |
| 153 | + Interval = 4 * time.Second |
| 154 | + ) |
| 155 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) |
| 156 | + |
| 157 | + if resp.Diagnostics.HasError() { |
| 158 | + return |
| 159 | + } |
| 160 | + agents := []string{} |
| 161 | + if !data.Agents.IsNull() { |
| 162 | + for _, agent := range data.Agents.Elements() { |
| 163 | + agents = append(agents, strings.Trim(agent.String(), "\"")) |
| 164 | + } |
| 165 | + } |
| 166 | + start := time.Now() |
| 167 | + err := r.client.OnboardAdoServerRepoToSpace(data.SpaceName.ValueString(), data.RepositoryName.ValueString(), |
| 168 | + data.RepositoryUrl.ValueString(), data.Token.ValueStringPointer(), data.Branch.ValueString(), data.CredentialName.ValueString(), agents, data.UseAllAgents.ValueBool(), data.AutoRegisterEac.ValueBool()) |
| 169 | + if err != nil { |
| 170 | + repo, err := r.client.GetRepoDetails(data.SpaceName.ValueString(), data.RepositoryName.ValueString()) |
| 171 | + if repo == nil { |
| 172 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to onboard repository to space, got error: %s", err)) |
| 173 | + return |
| 174 | + } |
| 175 | + if repo.Status == StatusSyncing { |
| 176 | + timeout := time.Duration(data.TimeOut.ValueInt32()) * time.Minute |
| 177 | + for time.Since(start) < timeout { |
| 178 | + repo, err := r.client.GetRepoDetails(data.SpaceName.ValueString(), data.RepositoryName.ValueString()) |
| 179 | + if err != nil { |
| 180 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Error while polling repository status: %s", err)) |
| 181 | + return |
| 182 | + } |
| 183 | + if repo.Status == StatusConnected { |
| 184 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 185 | + return |
| 186 | + } |
| 187 | + time.Sleep(Interval) |
| 188 | + } |
| 189 | + resp.Diagnostics.AddError("Sync Timeout", "Timed out while syncing repository") |
| 190 | + return |
| 191 | + } |
| 192 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to onboard repository to space, got error: %s", err)) |
| 193 | + return |
| 194 | + } |
| 195 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 196 | +} |
| 197 | + |
| 198 | +func (r *TorqueSpaceAdoServerRepositoryResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 199 | + var data TorqueSpaceAdoServerRepositoryResourceModel |
| 200 | + |
| 201 | + // Read Terraform prior state data into the model. |
| 202 | + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) |
| 203 | + |
| 204 | + if resp.Diagnostics.HasError() { |
| 205 | + return |
| 206 | + } |
| 207 | + |
| 208 | + // If applicable, this is a great opportunity to initialize any necessary |
| 209 | + // provider client data and make a call using it. |
| 210 | + |
| 211 | + // Save updated data into Terraform state. |
| 212 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 213 | +} |
| 214 | + |
| 215 | +func (r *TorqueSpaceAdoServerRepositoryResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 216 | + var data TorqueSpaceAdoServerRepositoryResourceModel |
| 217 | + |
| 218 | + // Read Terraform plan data into the model |
| 219 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) |
| 220 | + |
| 221 | + if resp.Diagnostics.HasError() { |
| 222 | + return |
| 223 | + } |
| 224 | + agents := []string{} |
| 225 | + if !data.Agents.IsNull() { |
| 226 | + for _, agent := range data.Agents.Elements() { |
| 227 | + agents = append(agents, strings.Trim(agent.String(), "\"")) |
| 228 | + } |
| 229 | + } |
| 230 | + err := r.client.UpdateRepoConfiguration(data.SpaceName.ValueString(), data.RepositoryName.ValueString(), |
| 231 | + data.CredentialName.ValueString(), agents, data.UseAllAgents.ValueBool()) |
| 232 | + if err != nil { |
| 233 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update repository configuration, got error: %s", err)) |
| 234 | + return |
| 235 | + } |
| 236 | + |
| 237 | + // Save updated data into Terraform state |
| 238 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 239 | +} |
| 240 | + |
| 241 | +func (r *TorqueSpaceAdoServerRepositoryResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 242 | + var data TorqueSpaceAdoServerRepositoryResourceModel |
| 243 | + |
| 244 | + // Read Terraform prior state data into the model. |
| 245 | + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) |
| 246 | + |
| 247 | + if resp.Diagnostics.HasError() { |
| 248 | + return |
| 249 | + } |
| 250 | + |
| 251 | + // Remove repo from space. |
| 252 | + err := r.client.RemoveRepoFromSpace(data.SpaceName.ValueString(), data.RepositoryName.ValueString()) |
| 253 | + if err != nil { |
| 254 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to remove repository from space, got error: %s", err)) |
| 255 | + return |
| 256 | + } |
| 257 | + |
| 258 | +} |
| 259 | + |
| 260 | +func (r *TorqueSpaceAdoServerRepositoryResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { |
| 261 | + resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp) |
| 262 | +} |
0 commit comments