Skip to content

Commit

Permalink
Reduce duplication in Import block (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
taiidani authored Oct 13, 2023
1 parent fc8c57f commit ef81de5
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 95 deletions.
25 changes: 25 additions & 0 deletions jenkins/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package jenkins
import (
"context"
"fmt"
"strings"

"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"
Expand Down Expand Up @@ -45,6 +47,29 @@ func (r *resourceHelper) Configure(ctx context.Context, req resource.ConfigureRe
r.client = client
}

// ImportState is called when performing import operations of existing resources.
func (r *resourceHelper) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
splitID := strings.Split(req.ID, "/")
if len(splitID) < 2 {
resp.Diagnostics.AddError(
"Unexpected Import Identifier",
fmt.Sprintf("Expected import identifier with format: \"[<folder>/]<domain>/<name>\". Got: %q", req.ID),
)
return
}

name := splitID[len(splitID)-1]
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("name"), name)...)

domain := splitID[len(splitID)-2]
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("domain"), domain)...)

folder := strings.Trim(strings.Join(splitID[0:len(splitID)-2], "/"), "/")
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("folder"), folder)...)

resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), generateCredentialID(folder, name))...)
}

func (r *resourceHelper) schema(s map[string]schema.Attribute) map[string]schema.Attribute {
s["id"] = schema.StringAttribute{
Computed: true,
Expand Down
23 changes: 0 additions & 23 deletions jenkins/resource_jenkins_credential_azure_service_principal.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,26 +346,3 @@ func (r *credentialAzureServicePrincipalResource) Delete(ctx context.Context, re
return
}
}

// ImportState is called when performing import operations of existing resources.
func (r *credentialAzureServicePrincipalResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
splitID := strings.Split(req.ID, "/")
if len(splitID) < 2 {
resp.Diagnostics.AddError(
"Unexpected Import Identifier",
fmt.Sprintf("Expected import identifier with format: \"[<folder>/]<domain>/<name>\". Got: %q", req.ID),
)
return
}

name := splitID[len(splitID)-1]
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("name"), name)...)

domain := splitID[len(splitID)-2]
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("domain"), domain)...)

folder := strings.Trim(strings.Join(splitID[0:len(splitID)-2], "/"), "/")
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("folder"), folder)...)

resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), generateCredentialID(folder, name))...)
}
24 changes: 0 additions & 24 deletions jenkins/resource_jenkins_credential_secret_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

jenkins "github.com/bndr/gojenkins"
"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/types"
Expand Down Expand Up @@ -231,26 +230,3 @@ func (r *credentialSecretFileResource) Delete(ctx context.Context, req resource.
return
}
}

// ImportState is called when performing import operations of existing resources.
func (r *credentialSecretFileResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
splitID := strings.Split(req.ID, "/")
if len(splitID) < 2 {
resp.Diagnostics.AddError(
"Unexpected Import Identifier",
fmt.Sprintf("Expected import identifier with format: \"[<folder>/]<domain>/<name>\". Got: %q", req.ID),
)
return
}

name := splitID[len(splitID)-1]
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("name"), name)...)

domain := splitID[len(splitID)-2]
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("domain"), domain)...)

folder := strings.Trim(strings.Join(splitID[0:len(splitID)-2], "/"), "/")
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("folder"), folder)...)

resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), generateCredentialID(folder, name))...)
}
24 changes: 0 additions & 24 deletions jenkins/resource_jenkins_credential_username.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

jenkins "github.com/bndr/gojenkins"
"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/types"
Expand Down Expand Up @@ -232,26 +231,3 @@ func (r *credentialUsernameResource) Delete(ctx context.Context, req resource.De
return
}
}

// ImportState is called when performing import operations of existing resources.
func (r *credentialUsernameResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
splitID := strings.Split(req.ID, "/")
if len(splitID) < 2 {
resp.Diagnostics.AddError(
"Unexpected Import Identifier",
fmt.Sprintf("Expected import identifier with format: \"[<folder>/]<domain>/<name>\". Got: %q", req.ID),
)
return
}

name := splitID[len(splitID)-1]
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("name"), name)...)

domain := splitID[len(splitID)-2]
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("domain"), domain)...)

folder := strings.Trim(strings.Join(splitID[0:len(splitID)-2], "/"), "/")
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("folder"), folder)...)

resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), generateCredentialID(folder, name))...)
}
24 changes: 0 additions & 24 deletions jenkins/resource_jenkins_credential_vault_approle.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"strings"

"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/stringdefault"
Expand Down Expand Up @@ -267,26 +266,3 @@ func (r *credentialVaultAppRoleResource) Delete(ctx context.Context, req resourc
return
}
}

// ImportState is called when performing import operations of existing resources.
func (r *credentialVaultAppRoleResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
splitID := strings.Split(req.ID, "/")
if len(splitID) < 2 {
resp.Diagnostics.AddError(
"Unexpected Import Identifier",
fmt.Sprintf("Expected import identifier with format: \"[<folder>/]<domain>/<name>\". Got: %q", req.ID),
)
return
}

name := splitID[len(splitID)-1]
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("name"), name)...)

domain := splitID[len(splitID)-2]
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("domain"), domain)...)

folder := strings.Trim(strings.Join(splitID[0:len(splitID)-2], "/"), "/")
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("folder"), folder)...)

resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), generateCredentialID(folder, name))...)
}

0 comments on commit ef81de5

Please sign in to comment.