Skip to content

Commit

Permalink
Add team_api_key resource
Browse files Browse the repository at this point in the history
  • Loading branch information
majori committed Apr 8, 2024
1 parent 6c2fdd0 commit b930632
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 3 deletions.
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
171 changes: 171 additions & 0 deletions internal/provider/team_api_key_resource.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit b930632

Please sign in to comment.