From b930632fa98f092bb51f38e673f2c915605c6a3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20Kivim=C3=A4ki?= Date: Mon, 8 Apr 2024 10:25:14 +0300 Subject: [PATCH] Add team_api_key resource --- docker-compose.yml | 10 ++ go.mod | 2 +- go.sum | 4 +- internal/provider/provider.go | 1 + internal/provider/team_api_key_resource.go | 171 +++++++++++++++++++++ 5 files changed, 185 insertions(+), 3 deletions(-) create mode 100644 internal/provider/team_api_key_resource.go diff --git a/docker-compose.yml b/docker-compose.yml index b7a9418..7ea52d7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -130,3 +130,13 @@ services: ports: - "8080:8080" restart: unless-stopped + + swagger: + image: swaggerapi/swagger-ui + depends_on: + - dtrack-apiserver + environment: + - SWAGGER_JSON_URL=http://localhost:8081/api/swagger.json + ports: + - "80:8080" + restart: unless-stopped diff --git a/go.mod b/go.mod index 50de569..295d796 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/futurice/terraform-provider-dependencytrack go 1.21 require ( - github.com/futurice/dependency-track-client-go v0.0.0-20240322102355-485c6eed2e38 + github.com/futurice/dependency-track-client-go v0.0.0-20240408064444-9e48d412f6d3 github.com/google/uuid v1.6.0 github.com/hashicorp/terraform-plugin-docs v0.18.0 github.com/hashicorp/terraform-plugin-framework v1.7.0 diff --git a/go.sum b/go.sum index 543b2a1..03825dd 100644 --- a/go.sum +++ b/go.sum @@ -34,8 +34,8 @@ github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE= github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps= -github.com/futurice/dependency-track-client-go v0.0.0-20240322102355-485c6eed2e38 h1:lDP0KV8wctzOqtd1rOkvex9TfecQ9bx/J66n8XCDTaw= -github.com/futurice/dependency-track-client-go v0.0.0-20240322102355-485c6eed2e38/go.mod h1:nSSUhNjXItvlllTmfE3BdooP1GtAuu6dDXFAHSem0Jk= +github.com/futurice/dependency-track-client-go v0.0.0-20240408064444-9e48d412f6d3 h1:LseLMUNGMHS0yvnMBWdIgBkpZrlVCCacCaDk+YDDghw= +github.com/futurice/dependency-track-client-go v0.0.0-20240408064444-9e48d412f6d3/go.mod h1:nSSUhNjXItvlllTmfE3BdooP1GtAuu6dDXFAHSem0Jk= github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI= github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic= github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU= diff --git a/internal/provider/provider.go b/internal/provider/provider.go index b960755..0a6b637 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -98,6 +98,7 @@ func (p *DependencyTrackProvider) Configure(ctx context.Context, req provider.Co func (p *DependencyTrackProvider) Resources(ctx context.Context) []func() resource.Resource { return []func() resource.Resource{ NewTeamResource, + NewTeamAPIKeyResource, NewTeamPermissionResource, NewProjectResource, NewACLMappingResource, diff --git a/internal/provider/team_api_key_resource.go b/internal/provider/team_api_key_resource.go new file mode 100644 index 0000000..9ea6684 --- /dev/null +++ b/internal/provider/team_api_key_resource.go @@ -0,0 +1,171 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package provider + +import ( + "context" + "fmt" + + dtrack "github.com/futurice/dependency-track-client-go" + "github.com/google/uuid" + + "github.com/hashicorp/terraform-plugin-framework/path" + "github.com/hashicorp/terraform-plugin-framework/resource" + "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" + "github.com/hashicorp/terraform-plugin-framework/types" +) + +// Ensure provider defined types fully satisfy framework interfaces. +var _ resource.Resource = &TeamAPIKeyResource{} +var _ resource.ResourceWithImportState = &TeamAPIKeyResource{} + +func NewTeamAPIKeyResource() resource.Resource { + return &TeamAPIKeyResource{} +} + +// TeamAPIKeyResource defines the resource implementation. +type TeamAPIKeyResource struct { + client *dtrack.Client +} + +// TeamAPIKeyResourceModel describes the resource data model. +type TeamAPIKeyResourceModel struct { + TeamID types.String `tfsdk:"team_id"` + Value types.String `tfsdk:"value"` +} + +func (r *TeamAPIKeyResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_team_api_key" +} + +func (r *TeamAPIKeyResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { + resp.Schema = schema.Schema{ + MarkdownDescription: "Team permission", + + Attributes: map[string]schema.Attribute{ + "team_id": schema.StringAttribute{ + MarkdownDescription: "ID of the team", + Required: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.RequiresReplace(), + }, + }, + "value": schema.StringAttribute{ + MarkdownDescription: "Value of the API key", + Computed: true, + Sensitive: true, + }, + }, + } +} + +func (r *TeamAPIKeyResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + client, ok := req.ProviderData.(*dtrack.Client) + + if !ok { + resp.Diagnostics.AddError( + "Unexpected Resource Configure Type", + fmt.Sprintf("Expected *dtrack.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), + ) + + return + } + + r.client = client +} + +func (r *TeamAPIKeyResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var plan TeamAPIKeyResourceModel + + resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) + + if resp.Diagnostics.HasError() { + return + } + + apiKey, err := r.client.Team.GenerateAPIKey(ctx, uuid.MustParse(plan.TeamID.String())) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create API key, got error: %s", err)) + return + } + + plan.Value = types.StringValue(apiKey) + + resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...) +} + +func (r *TeamAPIKeyResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var state TeamAPIKeyResourceModel + + resp.Diagnostics.Append(req.State.Get(ctx, &state)...) + + if resp.Diagnostics.HasError() { + return + } + + // NOTE: API only returns the API keys for the team when fetching all the teams + teams, err := r.client.Team.GetAll(ctx, dtrack.PageOptions{}) + if err != nil { + if apiErr, ok := err.(*dtrack.APIError); ok && apiErr.StatusCode == 404 { + resp.State.RemoveResource(ctx) + return + } + + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read team, got error: %s", err)) + return + } + + found := false + for _, team := range teams.Items { + if team.UUID.String() != state.TeamID.ValueString() { + continue + } + + for _, key := range team.APIKeys { + if key.Key == state.Value.ValueString() { + found = true + break + } + } + } + + if !found { + resp.State.RemoveResource(ctx) + return + } + + resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) +} + +func (r *TeamAPIKeyResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddError("Internal Error", "API Key resource is immutable") +} + +func (r *TeamAPIKeyResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var state TeamAPIKeyResourceModel + + resp.Diagnostics.Append(req.State.Get(ctx, &state)...) + + if resp.Diagnostics.HasError() { + return + } + + _, err := r.client.Team.DeleteAPIKey(ctx, state.Value.ValueString()) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete API key, got error: %s", err)) + return + } + + resp.State.RemoveResource(ctx) +} + +func (r *TeamAPIKeyResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { + resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) +}