Skip to content

Commit

Permalink
fix: default values
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Theuermann <[email protected]>
  • Loading branch information
mati007thm committed Dec 12, 2024
1 parent 145a788 commit da1900b
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions internal/provider/integration_github_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import (

"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"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/listdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand Down Expand Up @@ -121,28 +124,34 @@ func (r *integrationGithubResource) Schema(ctx context.Context, req resource.Sch
"repository": schema.StringAttribute{
MarkdownDescription: "GitHub Repository.",
Optional: true,
Computed: true,
Default: stringdefault.StaticString(""),
},
"repository_allow_list": schema.ListAttribute{
MarkdownDescription: "List of GitHub repositories to scan.",
Optional: true,
Computed: true,
ElementType: types.StringType,
Validators: []validator.List{
// Validate only this attribute or other_attr is configured.
listvalidator.ConflictsWith(path.Expressions{
path.MatchRoot("repository_deny_list"),
}...),
},
Default: listdefault.StaticValue(types.ListValueMust(types.StringType, []attr.Value{})),
},
"repository_deny_list": schema.ListAttribute{
MarkdownDescription: "List of GitHub repositories to exclude from scanning.",
Optional: true,
Computed: true,
ElementType: types.StringType,
Validators: []validator.List{
// Validate only this attribute or other_attr is configured.
listvalidator.ConflictsWith(path.Expressions{
path.MatchRoot("repository_allow_list"),
}...),
},
Default: listdefault.StaticValue(types.ListValueMust(types.StringType, []attr.Value{})),
},
"credentials": schema.SingleNestedAttribute{
Required: true,
Expand Down Expand Up @@ -256,29 +265,28 @@ func (r *integrationGithubResource) Read(ctx context.Context, req resource.ReadR
return
}

data.Name = types.StringValue(integration.Name)
data.Owner = types.StringValue(integration.ConfigurationOptions.GithubConfigurationOptions.Owner)
if integration.ConfigurationOptions.GithubConfigurationOptions.Repository != "" {
data.Repository = types.StringValue(integration.ConfigurationOptions.GithubConfigurationOptions.Repository)
} else {
data.Repository = types.StringNull()
}
allowList := ConvertListValue(integration.ConfigurationOptions.GithubConfigurationOptions.ReposAllowList)
denyList := ConvertListValue(integration.ConfigurationOptions.GithubConfigurationOptions.ReposDenyList)

// Handle allow list and deny list
if len(integration.ConfigurationOptions.GithubConfigurationOptions.ReposAllowList) == 0 {
data.RepositoryAllowList = types.ListNull(types.StringType)
} else {
data.RepositoryAllowList = ConvertListValue(integration.ConfigurationOptions.GithubConfigurationOptions.ReposAllowList)
model := integrationGithubResourceModel{
Mrn: types.StringValue(integration.Mrn),
Name: types.StringValue(integration.Name),
SpaceID: types.StringValue(integration.SpaceID()),
Owner: types.StringValue(integration.ConfigurationOptions.GithubConfigurationOptions.Owner),
Repository: types.StringValue(integration.ConfigurationOptions.GithubConfigurationOptions.Repository),
RepositoryAllowList: allowList,
RepositoryDenyList: denyList,
Credential: &integrationGithubCredentialModel{
Token: types.StringValue(data.Credential.Token.ValueString()),
},
}

if len(integration.ConfigurationOptions.GithubConfigurationOptions.ReposDenyList) == 0 {
data.RepositoryDenyList = types.ListNull(types.StringType)
} else {
data.RepositoryDenyList = ConvertListValue(integration.ConfigurationOptions.GithubConfigurationOptions.ReposDenyList)
if model.Owner.ValueString() == "" {
model.Owner = types.StringValue(integration.ConfigurationOptions.GithubConfigurationOptions.Organization)
}

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
resp.Diagnostics.Append(resp.State.Set(ctx, &model)...)
}

func (r *integrationGithubResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
Expand Down

0 comments on commit da1900b

Please sign in to comment.