Skip to content

Commit

Permalink
feat: add registry resource and update humanitec client
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjenek committed Dec 13, 2023
1 parent 690891c commit 1d5d3c9
Show file tree
Hide file tree
Showing 9 changed files with 526 additions and 63 deletions.
1 change: 1 addition & 0 deletions examples/resources/humanitec_registry/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import humanitec_registry.example registry_id
12 changes: 12 additions & 0 deletions examples/resources/humanitec_registry/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resource "humanitec_registry" "example" {
id = "example-registry"
registry = "registry.example.com"
type = "secret_ref"
enable_ci = true
secrets = {
cluster-a = {
namespace = "example-namespace"
secret = "path/to/secret"
}
}
}
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/hashicorp/terraform-plugin-go v0.19.0
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.29.0
github.com/humanitec/humanitec-go-autogen v0.0.0-20231011074422-e4592591bb03
github.com/humanitec/humanitec-go-autogen v0.0.0-20231213120624-80322ffb0f43
github.com/stretchr/testify v1.8.4
)

Expand Down Expand Up @@ -53,7 +53,7 @@ require (
github.com/golang/snappy v0.0.4 // indirect
github.com/gomarkdown/markdown v0.0.0-20230922112808-5421fefb8386 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
Expand Down Expand Up @@ -85,7 +85,7 @@ require (
github.com/kataras/tunnel v0.0.4 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/labstack/echo/v4 v4.11.2 // indirect
github.com/labstack/echo/v4 v4.11.3 // indirect
github.com/labstack/gommon v0.4.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mailgun/raymond/v2 v2.0.48 // indirect
Expand All @@ -101,6 +101,7 @@ require (
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/oapi-codegen/runtime v1.1.0 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down Expand Up @@ -135,5 +136,6 @@ require (
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
Expand Down Expand Up @@ -189,6 +191,8 @@ github.com/huandu/xstrings v1.4.0 h1:D17IlohoQq4UcpqD7fDk80P7l+lwAmlFaBHgOipl2FU
github.com/huandu/xstrings v1.4.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/humanitec/humanitec-go-autogen v0.0.0-20231011074422-e4592591bb03 h1:EN2UREH8bXYtRaPEq93tkN0TTkowWFKOXdXGQ6YgkLs=
github.com/humanitec/humanitec-go-autogen v0.0.0-20231011074422-e4592591bb03/go.mod h1:RZj+jfsRy1Z6sGPlfEYPFXl+hLW9nGo/4VQzm35yqs8=
github.com/humanitec/humanitec-go-autogen v0.0.0-20231213120624-80322ffb0f43 h1:8KuHxEUwcJGsK1UGFek/3daXOkGgpz1qDguP2GIhHjQ=
github.com/humanitec/humanitec-go-autogen v0.0.0-20231213120624-80322ffb0f43/go.mod h1:QmeSmyBNgMIbbJ6b4MefN0aEOUpMXqZzIuvlMqEYKus=
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/imdario/mergo v0.3.15 h1:M8XP7IuFNsqUx6VPK2P9OSmsYsI/YFaGil0uD21V3dM=
github.com/imdario/mergo v0.3.15/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
Expand Down Expand Up @@ -238,6 +242,7 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/labstack/echo/v4 v4.11.2 h1:T+cTLQxWCDfqDEoydYm5kCobjmHwOwcv4OJAPHilmdE=
github.com/labstack/echo/v4 v4.11.2/go.mod h1:UcGuQ8V6ZNRmSweBIJkPvGfwCMIlFmiqrPqiEBfPYws=
github.com/labstack/echo/v4 v4.11.3/go.mod h1:UcGuQ8V6ZNRmSweBIJkPvGfwCMIlFmiqrPqiEBfPYws=
github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
Expand Down Expand Up @@ -281,6 +286,8 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/oapi-codegen/runtime v1.1.0 h1:rJpoNUawn5XTvekgfkvSZr0RqEnoYpFkyvrzfWeFKWM=
github.com/oapi-codegen/runtime v1.1.0/go.mod h1:BeSfBkWWWnAnGdyS+S/GnlbmHKzf8/hwkvelJZDeKA8=
github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA=
github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
Expand Down
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func (p *HumanitecProvider) Resources(ctx context.Context) []func() resource.Res
NewResourceEnvironmentTypeUser(true),
NewResourceEnvironmentTypeUser(false),
NewResourcePipeline,
NewResourceRegistry,
NewResourceResourceDriver,
NewResourceRule,
NewResourceSecretStore,
Expand Down
95 changes: 40 additions & 55 deletions internal/provider/resource_artefact_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"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/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/humanitec/humanitec-go-autogen"
Expand Down Expand Up @@ -63,6 +65,9 @@ func (r *ResourceArtefactVersion) Schema(ctx context.Context, req resource.Schem
"type": schema.StringAttribute{
MarkdownDescription: "The Artefact Version type.",
Required: true,
Validators: []validator.String{
stringvalidator.OneOf("container"),
},
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
Expand Down Expand Up @@ -128,15 +133,17 @@ func setOptionalStringValue(target types.String, source string) types.String {
return types.StringValue(source)
}

func parseArtefactVersionResponse(res *client.ArtefactVersionResponse, artefactRes *client.ArtefactResponse, data *ArtefactVersionModel) {
func parseArtefactVersionResponse(res client.ContainerArtefactVersion, data *ArtefactVersionModel) {
data.ID = types.StringValue(res.Id)
data.Name = types.StringValue(res.Name)
data.Type = types.StringValue(artefactRes.Type)
data.Type = types.StringValue("container")

data.Commit = setOptionalStringValue(data.Commit, res.Commit)
data.Digest = setOptionalStringValue(data.Digest, res.Digest)
data.Ref = setOptionalStringValue(data.Ref, res.Ref)
data.Version = setOptionalStringValue(data.Version, res.Version)
if res.Version != nil {
data.Version = setOptionalStringValue(data.Version, *res.Version)
}
}

func (r *ResourceArtefactVersion) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
Expand All @@ -149,53 +156,46 @@ func (r *ResourceArtefactVersion) Create(ctx context.Context, req resource.Creat
return
}

httpResp, err := r.client.PostOrgsOrgIdArtefactVersionsWithResponse(ctx, r.orgId, &client.PostOrgsOrgIdArtefactVersionsParams{}, client.PostOrgsOrgIdArtefactVersionsJSONRequestBody{
artefactContainerRequest := client.CreateContainerArtefactVersion{
Commit: data.Commit.ValueStringPointer(),
Digest: data.Digest.ValueStringPointer(),
Name: data.Name.ValueString(),
Ref: data.Ref.ValueStringPointer(),
Type: data.Type.ValueString(),
Version: data.Version.ValueStringPointer(),
})
if err != nil {
resp.Diagnostics.AddError(HUM_CLIENT_ERR, fmt.Sprintf("Unable to create artefact version, got error: %s", err))
return
}

if httpResp.StatusCode() != 200 {
resp.Diagnostics.AddError(HUM_API_ERR, fmt.Sprintf("Unable to create artefact version, unexpected status code: %d, body: %s", httpResp.StatusCode(), httpResp.Body))
return
artefactRequest := client.CreateArtefactVersionJSONRequestBody{
Type: "container",
}

artefactHttpResp, err := r.client.GetOrgsOrgIdArtefactsWithResponse(ctx, r.orgId, &client.GetOrgsOrgIdArtefactsParams{
Name: &httpResp.JSON200.Name,
})
err := artefactRequest.MergeCreateContainerArtefactVersion(artefactContainerRequest)
if err != nil {
resp.Diagnostics.AddError(HUM_CLIENT_ERR, fmt.Sprintf("Unable to fetch created artefact, got error: %s", err))
return
resp.Diagnostics.AddError(
"Unexpected error",
fmt.Sprintf("Failed to create a request for container artefact creation: %v", err),
)
}

if httpResp.StatusCode() != 200 {
resp.Diagnostics.AddError(HUM_API_ERR, fmt.Sprintf("Unable to fetch created artefact, unexpected status code: %d, body: %s", httpResp.StatusCode(), httpResp.Body))
createArtefactVersionResp, err := r.client.CreateArtefactVersionWithResponse(ctx, r.orgId, &client.CreateArtefactVersionParams{}, artefactRequest)
if err != nil {
resp.Diagnostics.AddError(HUM_CLIENT_ERR, fmt.Sprintf("Unable to create artefact version, got error: %s", err))
return
}

artefacts := *artefactHttpResp.JSON200
artefactId := httpResp.JSON200.ArtefactId
var artefact *client.ArtefactResponse
for _, a := range artefacts {
if a.Id == artefactId {
artefact = &a
break
}
if createArtefactVersionResp.StatusCode() != 200 {
resp.Diagnostics.AddError(HUM_API_ERR, fmt.Sprintf("Unable to create artefact version, unexpected status code: %d, body: %s", createArtefactVersionResp.StatusCode(), createArtefactVersionResp.Body))
return
}

if artefact == nil {
resp.Diagnostics.AddError(HUM_API_ERR, fmt.Sprintf("Unable to fetch created artefact, id (%s) not found in response, %v", artefactId, artefacts))
return
artefactContainerResponse, err := createArtefactVersionResp.JSON200.AsContainerArtefactVersion()
if err != nil {
resp.Diagnostics.AddError(
"Unexpected error",
fmt.Sprintf("Failed to read container artefact creation response: %v", err),
)
}

parseArtefactVersionResponse(httpResp.JSON200, artefact, data)
parseArtefactVersionResponse(artefactContainerResponse, data)

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
Expand All @@ -211,41 +211,26 @@ func (r *ResourceArtefactVersion) Read(ctx context.Context, req resource.ReadReq
return
}

httpResp, err := r.client.GetOrgsOrgIdArtefactVersionsArtefactVersionIdWithResponse(ctx, r.orgId, data.ID.ValueString())
getArtefactVersionResp, err := r.client.GetArtefactVersionWithResponse(ctx, r.orgId, data.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError(HUM_CLIENT_ERR, fmt.Sprintf("Unable to read ArtefactVersion, got error: %s", err))
return
}

if httpResp.StatusCode() != 200 {
resp.Diagnostics.AddError(HUM_API_ERR, fmt.Sprintf("Unable to read ArtefactVersion, unexpected status code: %d, body: %s", httpResp.StatusCode(), httpResp.Body))
if getArtefactVersionResp.StatusCode() != 200 {
resp.Diagnostics.AddError(HUM_API_ERR, fmt.Sprintf("Unable to read ArtefactVersion, unexpected status code: %d, body: %s", getArtefactVersionResp.StatusCode(), getArtefactVersionResp.Body))
return
}

artefactHttpResp, err := r.client.GetOrgsOrgIdArtefactsWithResponse(ctx, r.orgId, &client.GetOrgsOrgIdArtefactsParams{
Name: &httpResp.JSON200.Name,
})
artefactContainerResponse, err := getArtefactVersionResp.JSON200.AsContainerArtefactVersion()
if err != nil {
resp.Diagnostics.AddError(HUM_CLIENT_ERR, fmt.Sprintf("Unable to read artefact, got error: %s", err))
return
}

if httpResp.StatusCode() != 200 {
resp.Diagnostics.AddError(HUM_API_ERR, fmt.Sprintf("Unable to read artefact, unexpected status code: %d, body: %s", httpResp.StatusCode(), httpResp.Body))
return
}

artefactId := httpResp.JSON200.ArtefactId
artefact, found := findInSlicePtr(artefactHttpResp.JSON200, func(a client.ArtefactResponse) bool {
return a.Id == artefactId
})

if !found {
resp.Diagnostics.AddError(HUM_API_ERR, fmt.Sprintf("Unable to read artefact, id (%s) not found in response, %+v", artefactId, artefactHttpResp.JSON200))
return
resp.Diagnostics.AddError(
"Unexpected error",
fmt.Sprintf("Failed to read container artefact creation response: %v", err),
)
}

parseArtefactVersionResponse(httpResp.JSON200, &artefact, data)
parseArtefactVersionResponse(artefactContainerResponse, data)

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
Expand Down
10 changes: 5 additions & 5 deletions internal/provider/resource_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (r *ResourcePipeline) Create(ctx context.Context, req resource.CreateReques
appID := data.AppID.ValueString()
definition := data.Definition.ValueString()

var pipeline *client.PipelineResponse
var pipeline *client.Pipeline
createPipelineResp, err := r.client.CreatePipelineWithBodyWithResponse(ctx, r.orgID, appID, "application/x-yaml", strings.NewReader(definition))
if err != nil {
resp.Diagnostics.AddError(HUM_CLIENT_ERR, fmt.Sprintf("Unable to create pipeline, got error: %s", err))
Expand Down Expand Up @@ -165,7 +165,7 @@ func (r *ResourcePipeline) Read(ctx context.Context, req resource.ReadRequest, r
appID := data.AppID.ValueString()
id := data.ID.ValueString()

var pipeline *client.PipelineResponse
var pipeline *client.Pipeline
getPipelineResp, err := r.client.GetPipelineWithResponse(ctx, r.orgID, appID, id, &client.GetPipelineParams{})
if err != nil {
resp.Diagnostics.AddError(HUM_CLIENT_ERR, fmt.Sprintf("Unable to get pipeline, got error: %s", err))
Expand All @@ -190,7 +190,7 @@ func (r *ResourcePipeline) Read(ctx context.Context, req resource.ReadRequest, r
}

contentType := "application/x.humanitec-pipelines-v1.0+yaml"
getPipelineDefinitionResp, err := r.client.GetPipelineSchemaWithResponse(ctx, r.orgID, appID, id, &client.GetPipelineSchemaParams{
getPipelineDefinitionResp, err := r.client.GetPipelineDefinitionWithResponse(ctx, r.orgID, appID, id, &client.GetPipelineDefinitionParams{
Accept: &contentType,
})
if err != nil {
Expand Down Expand Up @@ -224,7 +224,7 @@ func (r *ResourcePipeline) Update(ctx context.Context, req resource.UpdateReques
id := state.ID.ValueString()
definition := data.Definition.ValueString()

var pipeline *client.PipelineResponse
var pipeline *client.Pipeline
updatePipelineResp, err := r.client.UpdatePipelineWithBodyWithResponse(ctx, r.orgID, appID, id, &client.UpdatePipelineParams{}, "application/x-yaml", strings.NewReader(definition))
if err != nil {
resp.Diagnostics.AddError(HUM_CLIENT_ERR, fmt.Sprintf("Unable to update pipeline, got error: %s", err))
Expand Down Expand Up @@ -318,7 +318,7 @@ func (r *ResourcePipeline) ImportState(ctx context.Context, req resource.ImportS
}
}

func parsePipelineResponse(ctx context.Context, res *client.PipelineResponse, data *PipelineModel) diag.Diagnostics {
func parsePipelineResponse(ctx context.Context, res *client.Pipeline, data *PipelineModel) diag.Diagnostics {
totalDiags := diag.Diagnostics{}

data.AppID = types.StringValue(res.AppId)
Expand Down
Loading

0 comments on commit 1d5d3c9

Please sign in to comment.