diff --git a/docs/resources/integration_aws.md b/docs/resources/integration_aws.md index 6ea5661..7822052 100644 --- a/docs/resources/integration_aws.md +++ b/docs/resources/integration_aws.md @@ -24,7 +24,7 @@ variable "aws_access_key" { } variable "aws_secret_key" { - description = "AWS access key" + description = "AWS secret key" type = string } diff --git a/examples/resources/mondoo_integration_aws/resource.tf b/examples/resources/mondoo_integration_aws/resource.tf index b0509ba..a901c2c 100644 --- a/examples/resources/mondoo_integration_aws/resource.tf +++ b/examples/resources/mondoo_integration_aws/resource.tf @@ -9,7 +9,7 @@ variable "aws_access_key" { } variable "aws_secret_key" { - description = "AWS access key" + description = "AWS secret key" type = string } @@ -33,4 +33,3 @@ resource "mondoo_integration_aws" "name" { } } } - diff --git a/go.mod b/go.mod index 8184a75..4cbab7f 100644 --- a/go.mod +++ b/go.mod @@ -10,8 +10,8 @@ require ( github.com/hashicorp/terraform-plugin-go v0.23.0 github.com/hashicorp/terraform-plugin-log v0.9.0 github.com/hashicorp/terraform-plugin-testing v1.8.0 - github.com/stretchr/testify v1.9.0 go.mondoo.com/mondoo-go v0.0.0-20240611114249-2c3b9b20e67a + gopkg.in/yaml.v2 v2.4.0 ) require ( @@ -34,7 +34,6 @@ require ( github.com/cli/safeexec v1.0.1 // indirect github.com/cli/shurcooL-graphql v0.0.4 // indirect github.com/cloudflare/circl v1.3.8 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect github.com/fatih/color v1.16.0 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/go-jose/go-jose/v3 v3.0.3 // indirect @@ -91,7 +90,6 @@ require ( github.com/muesli/termenv v0.15.2 // indirect github.com/oklog/run v1.0.0 // indirect github.com/oklog/ulid v1.3.1 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect github.com/posener/complete v1.2.3 // indirect github.com/rivo/uniseg v0.4.7 // indirect github.com/samber/lo v1.39.0 // indirect @@ -123,7 +121,6 @@ require ( google.golang.org/appengine v1.6.8 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect google.golang.org/grpc v1.63.2 // indirect - google.golang.org/protobuf v1.34.0 // indirect - gopkg.in/yaml.v2 v2.4.0 // indirect + google.golang.org/protobuf v1.34.2 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 7d0b067..29ad497 100644 --- a/go.sum +++ b/go.sum @@ -686,8 +686,8 @@ google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpAD google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.34.0 h1:Qo/qEd2RZPCf2nKuorzksSknv0d3ERwp1vFG38gSmH4= -google.golang.org/protobuf v1.34.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= +google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d/go.mod h1:cuepJuh7vyXfUyUwEgHQXw849cJrilpS5NeIjOWESAw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/internal/provider/custom_compliance_framework_resource.go b/internal/provider/custom_compliance_framework_resource.go index c803b4d..aba55ca 100644 --- a/internal/provider/custom_compliance_framework_resource.go +++ b/internal/provider/custom_compliance_framework_resource.go @@ -4,8 +4,8 @@ import ( "context" "fmt" "os" + "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/planmodifier" @@ -217,5 +217,24 @@ func (r *customComplianceFrameworkResource) Delete(ctx context.Context, req reso } func (r *customComplianceFrameworkResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { - resource.ImportStatePassthroughID(ctx, path.Root("mrn"), req, resp) + // resource.ImportStatePassthroughID(ctx, path.Root("mrn"), req, resp) + mrn := req.ID + splitMrn := strings.Split(mrn, "/") + spaceMrn := spacePrefix + splitMrn[len(splitMrn)-3] + spaceId := splitMrn[len(splitMrn)-3] + uid := splitMrn[len(splitMrn)-1] + + framework, err := r.client.GetFramework(ctx, spaceMrn, spaceId, uid) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get Compliance Framework, got error: %s", err)) + return + } + + model := customComplianceFrameworkResourceModel{ + Mrn: types.StringValue(string(framework.Mrn)), + DataUrl: types.StringPointerValue(nil), + SpaceId: types.StringValue(spaceId), + } + + resp.State.Set(ctx, &model) } diff --git a/internal/provider/custom_policy.go b/internal/provider/custom_policy.go index d834236..4ef4382 100644 --- a/internal/provider/custom_policy.go +++ b/internal/provider/custom_policy.go @@ -8,6 +8,7 @@ import ( "fmt" "hash/crc32" "os" + "strings" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/path" @@ -296,3 +297,39 @@ func (r *customPolicyResource) Delete(ctx context.Context, req resource.DeleteRe } } } + +func (r *customPolicyResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { + mrn := req.ID + splitMrn := strings.Split(mrn, "/") + spaceMrn := spacePrefix + splitMrn[len(splitMrn)-3] + spaceId := splitMrn[len(splitMrn)-3] + + policy, err := r.client.GetPolicy(ctx, mrn, spaceMrn) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get policy, got error: %s", err)) + return + } + + content, err := r.client.DownloadBundle(ctx, string(policy.Mrn)) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to download bundle, got error: %s", err)) + return + } + + mrns, _ := types.ListValueFrom(ctx, types.StringType, []string{mrn}) + + model := customPolicyResourceModel{ + SpaceId: types.StringValue(spaceId), + Mrns: mrns, + Overwrite: types.BoolValue(false), + Source: types.StringPointerValue(nil), + Content: types.StringValue(content), + Crc32Checksum: types.StringPointerValue(nil), + } + + checksum := newCrc32Checksum([]byte(content)) + + model.Crc32Checksum = types.StringValue(checksum) + + resp.State.Set(ctx, &model) +} diff --git a/internal/provider/custom_querypack.go b/internal/provider/custom_querypack.go index 3eac849..af70a21 100644 --- a/internal/provider/custom_querypack.go +++ b/internal/provider/custom_querypack.go @@ -7,6 +7,7 @@ import ( "context" "fmt" "os" + "strings" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/path" @@ -288,3 +289,39 @@ func (r *customQueryPackResource) Delete(ctx context.Context, req resource.Delet } } } + +func (r *customQueryPackResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { + mrn := req.ID + splitMrn := strings.Split(mrn, "/") + spaceMrn := spacePrefix + splitMrn[len(splitMrn)-3] + spaceId := splitMrn[len(splitMrn)-3] + + queryPack, err := r.client.GetPolicy(ctx, mrn, spaceMrn) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get policy, got error: %s", err)) + return + } + + content, err := r.client.DownloadBundle(ctx, string(queryPack.Mrn)) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to download bundle, got error: %s", err)) + return + } + + mrns, _ := types.ListValueFrom(ctx, types.StringType, []string{mrn}) + + model := customQueryPackResourceModel{ + SpaceId: types.StringValue(spaceId), + Mrns: mrns, + Overwrite: types.BoolValue(false), + Source: types.StringPointerValue(nil), + Content: types.StringValue(content), + Crc32Checksum: types.StringPointerValue(nil), + } + + checksum := newCrc32Checksum([]byte(content)) + + model.Crc32Checksum = types.StringValue(checksum) + + resp.State.Set(ctx, &model) +} diff --git a/internal/provider/gql.go b/internal/provider/gql.go index 529145a..487a9f5 100644 --- a/internal/provider/gql.go +++ b/internal/provider/gql.go @@ -168,6 +168,7 @@ type Policy struct { IsPublic mondoov1.Boolean CreatedAt mondoov1.String UpdatedAt mondoov1.String + Docs mondoov1.String } type PolicyNode struct { @@ -250,6 +251,28 @@ type ContentPayload struct { Content Content } +func (c *ExtendedGqlClient) DownloadBundle(ctx context.Context, policyMrn string) (string, error) { + var q struct { + DownloadBundle struct { + PolicyBundleYaml struct { + Yaml string `graphql:"yaml"` + } `graphql:"... on PolicyBundleYaml"` + } `graphql:"downloadBundle(input: $input)"` + } + variables := map[string]interface{}{ + "input": mondoov1.DownloadBundleInput{ + Mrn: mondoov1.String(policyMrn), + }, + } + + err := c.Query(ctx, &q, variables) + if err != nil { + return "", err + } + + return q.DownloadBundle.PolicyBundleYaml.Yaml, nil +} + func (c *ExtendedGqlClient) GetPolicies(ctx context.Context, scopeMrn string, catalogType string, assignedOnly bool) (*[]Policy, error) { // Define the query struct according to the provided query var contentQuery struct { @@ -284,6 +307,28 @@ func (c *ExtendedGqlClient) GetPolicies(ctx context.Context, scopeMrn string, ca return &policies, nil } +func (c *ExtendedGqlClient) GetPolicy(ctx context.Context, policyMrn string, spaceMrn string) (*Policy, error) { + var q struct { + Policy Policy `graphql:"policy(input: $input)"` + } + + input := mondoov1.PolicyInput{ + Mrn: mondoov1.NewStringPtr(mondoov1.String(policyMrn)), + SpaceMrn: mondoov1.NewStringPtr(mondoov1.String(spaceMrn)), + } + + variables := map[string]interface{}{ + "input": input, + } + + err := c.Query(ctx, &q, variables) + if err != nil { + return nil, err + } + + return &q.Policy, nil +} + func (c *ExtendedGqlClient) AssignPolicy(ctx context.Context, spaceMrn string, action mondoov1.PolicyAction, policyMrns []string) error { var list *[]mondoov1.String @@ -448,6 +493,85 @@ func (c *ExtendedGqlClient) DeleteIntegration(ctx context.Context, mrn string) ( return &deleteMutation.DeleteClientIntegration, nil } +type AzureConfigurationOptions struct { + TenantId string + ClientId string + SubscriptionsWhitelist []string + SubscriptionsBlacklist []string + ScanVms bool +} + +type HostConfigurationOptions struct { + Host string `graphql:"host"` + HTTPS bool `graphql:"https"` + HTTP bool `graphql:"http"` +} + +type SlackConfigurationOptions struct { + Placeholder string +} + +type GithubConfigurationOptions struct { + Owner string + Repository string + Organization string + Type string + ReposAllowList []string + ReposDenyList []string +} + +type Ms365ConfigurationOptions struct { + TenantId string + ClientId string +} + +type HostedAwsConfigurationOptions struct { + AccessKeyId string + Role string +} + +type GcpConfigurationOptions struct { + ProjectId string + DiscoverAll bool +} + +type ClientIntegrationConfigurationOptions struct { + AzureConfigurationOptions AzureConfigurationOptions `graphql:"... on AzureConfigurationOptions"` + HostConfigurationOptions HostConfigurationOptions `graphql:"... on HostConfigurationOptions"` + Ms365ConfigurationOptions Ms365ConfigurationOptions `graphql:"... on Ms365ConfigurationOptions"` + GcpConfigurationOptions GcpConfigurationOptions `graphql:"... on GcpConfigurationOptions"` + SlackConfigurationOptions SlackConfigurationOptions `graphql:"... on SlackConfigurationOptions"` + GithubConfigurationOptions GithubConfigurationOptions `graphql:"... on GithubConfigurationOptions"` + HostedAwsConfigurationOptions HostedAwsConfigurationOptions `graphql:"... on HostedAwsConfigurationOptions"` + // Add other configuration options here +} + +type Integration struct { + Mrn string + Name string + ConfigurationOptions ClientIntegrationConfigurationOptions `graphql:"configurationOptions"` +} + +type ClientIntegration struct { + Integration Integration +} + +func (c *ExtendedGqlClient) GetClientIntegration(ctx context.Context, mrn string) (Integration, error) { + var q struct { + ClientIntegration ClientIntegration `graphql:"clientIntegration(input: {mrn: $mrn})"` + } + variables := map[string]interface{}{ + "mrn": mondoov1.String(mrn), + } + + err := c.Query(ctx, &q, variables) + if err != nil { + return Integration{}, err + } + + return q.ClientIntegration.Integration, nil +} + type triggerActionPayload struct { Mrn string } diff --git a/internal/provider/integration_aws_resource.go b/internal/provider/integration_aws_resource.go index 7a0ba55..871201b 100644 --- a/internal/provider/integration_aws_resource.go +++ b/internal/provider/integration_aws_resource.go @@ -7,6 +7,7 @@ import ( "context" "fmt" "regexp" + "strings" "github.com/hashicorp/terraform-plugin-framework-validators/objectvalidator" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" @@ -279,5 +280,28 @@ func (r *integrationAwsResource) Delete(ctx context.Context, req resource.Delete } func (r *integrationAwsResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { - resource.ImportStatePassthroughID(ctx, path.Root("mrn"), req, resp) + mrn := req.ID + integration, err := r.client.GetClientIntegration(ctx, mrn) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to import AWS integration, got error: %s", err)) + return + } + + model := integrationAwsResourceModel{ + SpaceId: types.StringValue(strings.Split(integration.Mrn, "/")[len(strings.Split(integration.Mrn, "/"))-3]), + Mrn: types.StringValue(integration.Mrn), + Name: types.StringValue(integration.Name), + Credential: integrationAwsCredentialModel{ + Role: &roleCredentialModel{ + RoleArn: types.StringValue(integration.ConfigurationOptions.HostedAwsConfigurationOptions.Role), + ExternalId: types.StringPointerValue(nil), // cannot be imported + }, + Key: &accessKeyCredentialModel{ + AccessKey: types.StringValue(integration.ConfigurationOptions.HostedAwsConfigurationOptions.AccessKeyId), + SecretKey: types.StringPointerValue(nil), // cannot be imported + }, + }, + } + + resp.State.Set(ctx, &model) } diff --git a/internal/provider/integration_azure_resource.go b/internal/provider/integration_azure_resource.go index 6ce9799..2a77f3e 100644 --- a/internal/provider/integration_azure_resource.go +++ b/internal/provider/integration_azure_resource.go @@ -3,9 +3,11 @@ package provider import ( "context" "fmt" + "strings" "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" @@ -289,5 +291,38 @@ func (r *integrationAzureResource) Delete(ctx context.Context, req resource.Dele } func (r *integrationAzureResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { - resource.ImportStatePassthroughID(ctx, path.Root("mrn"), req, resp) + mrn := req.ID + integration, err := r.client.GetClientIntegration(ctx, mrn) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to import Azure integration, got error: %s", err)) + return + } + + allowList := r.ConvertListValue(ctx, integration.ConfigurationOptions.AzureConfigurationOptions.SubscriptionsWhitelist) + denyList := r.ConvertListValue(ctx, integration.ConfigurationOptions.AzureConfigurationOptions.SubscriptionsBlacklist) + + model := integrationAzureResourceModel{ + SpaceId: types.StringValue(strings.Split(integration.Mrn, "/")[len(strings.Split(integration.Mrn, "/"))-3]), + Mrn: types.StringValue(integration.Mrn), + Name: types.StringValue(integration.Name), + ClientId: types.StringValue(integration.ConfigurationOptions.AzureConfigurationOptions.ClientId), + TenantId: types.StringValue(integration.ConfigurationOptions.AzureConfigurationOptions.TenantId), + SubscriptionAllowList: allowList, + SubscriptionDenyList: denyList, + Credential: integrationAzureCredentialModel{ + PEMFile: types.StringPointerValue(nil), + }, + ScanVms: types.BoolValue(integration.ConfigurationOptions.AzureConfigurationOptions.ScanVms), + } + + resp.State.Set(ctx, &model) +} + +func (r *integrationAzureResource) ConvertListValue(ctx context.Context, list []string) types.List { + var valueList []attr.Value + for _, str := range list { + valueList = append(valueList, types.StringValue(str)) + } + // Ensure the list is of type types.StringType + return types.ListValueMust(types.StringType, valueList) } diff --git a/internal/provider/integration_domain_resource.go b/internal/provider/integration_domain_resource.go index 6f91b03..4c173bc 100644 --- a/internal/provider/integration_domain_resource.go +++ b/internal/provider/integration_domain_resource.go @@ -4,9 +4,9 @@ import ( "context" "fmt" "regexp" + "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" @@ -212,5 +212,20 @@ func (r *integrationDomainResource) Delete(ctx context.Context, req resource.Del } func (r *integrationDomainResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { - resource.ImportStatePassthroughID(ctx, path.Root("mrn"), req, resp) + mrn := req.ID + integration, err := r.client.GetClientIntegration(ctx, mrn) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to retrieve integration, got error: %s", err)) + return + } + + model := integrationDomainResourceModel{ + SpaceId: types.StringValue(strings.Split(integration.Mrn, "/")[len(strings.Split(integration.Mrn, "/"))-3]), + Mrn: types.StringValue(integration.Mrn), + Host: types.StringValue(integration.ConfigurationOptions.HostConfigurationOptions.Host), + Https: types.BoolValue(integration.ConfigurationOptions.HostConfigurationOptions.HTTPS), + Http: types.BoolValue(integration.ConfigurationOptions.HostConfigurationOptions.HTTP), + } + + resp.State.Set(ctx, &model) } diff --git a/internal/provider/integration_gcp_resource.go b/internal/provider/integration_gcp_resource.go index 1979928..8325ed8 100644 --- a/internal/provider/integration_gcp_resource.go +++ b/internal/provider/integration_gcp_resource.go @@ -6,9 +6,9 @@ package provider 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" @@ -221,5 +221,22 @@ func (r *integrationGcpResource) Delete(ctx context.Context, req resource.Delete } func (r *integrationGcpResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { - resource.ImportStatePassthroughID(ctx, path.Root("mrn"), req, resp) + mrn := req.ID + integration, err := r.client.GetClientIntegration(ctx, mrn) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get GCP integration, got error: %s", err)) + return + } + + model := integrationGcpResourceModel{ + Mrn: types.StringValue(integration.Mrn), + Name: types.StringValue(integration.Name), + SpaceId: types.StringValue(strings.Split(integration.Mrn, "/")[len(strings.Split(integration.Mrn, "/"))-3]), + ProjectId: types.StringValue(integration.ConfigurationOptions.GcpConfigurationOptions.ProjectId), + Credential: integrationGcpCredentialModel{ + PrivateKey: types.StringPointerValue(nil), + }, + } + + resp.State.Set(ctx, &model) } diff --git a/internal/provider/integration_github_resource.go b/internal/provider/integration_github_resource.go index efa2cae..c770383 100644 --- a/internal/provider/integration_github_resource.go +++ b/internal/provider/integration_github_resource.go @@ -4,9 +4,11 @@ import ( "context" "fmt" "regexp" + "strings" "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" @@ -42,7 +44,7 @@ type integrationGithubResourceModel struct { RepositoryDenyList types.List `tfsdk:"repository_deny_list"` // credentials - Credential integrationGithubCredentialModel `tfsdk:"credentials"` + Credential *integrationGithubCredentialModel `tfsdk:"credentials"` } type integrationGithubCredentialModel struct { @@ -285,5 +287,41 @@ func (r *integrationGithubResource) Delete(ctx context.Context, req resource.Del } func (r *integrationGithubResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { - resource.ImportStatePassthroughID(ctx, path.Root("mrn"), req, resp) + mrn := req.ID + integration, err := r.client.GetClientIntegration(ctx, mrn) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get GitHub integration, got error: %s", err)) + return + } + + allowList := r.ConvertListValue(ctx, integration.ConfigurationOptions.GithubConfigurationOptions.ReposAllowList) + denyList := r.ConvertListValue(ctx, integration.ConfigurationOptions.GithubConfigurationOptions.ReposDenyList) + + model := integrationGithubResourceModel{ + Mrn: types.StringValue(mrn), + Name: types.StringValue(integration.Name), + SpaceId: types.StringValue(strings.Split(integration.Mrn, "/")[len(strings.Split(integration.Mrn, "/"))-3]), + Owner: types.StringValue(integration.ConfigurationOptions.GithubConfigurationOptions.Owner), + Repository: types.StringValue(integration.ConfigurationOptions.GithubConfigurationOptions.Repository), + RepositoryAllowList: allowList, + RepositoryDenyList: denyList, + Credential: &integrationGithubCredentialModel{ + Token: types.StringPointerValue(nil), + }, + } + + if model.Owner.ValueString() == "" { + model.Owner = types.StringValue(integration.ConfigurationOptions.GithubConfigurationOptions.Organization) + } + + resp.State.Set(ctx, &model) +} + +func (r *integrationGithubResource) ConvertListValue(ctx context.Context, list []string) types.List { + var valueList []attr.Value + for _, str := range list { + valueList = append(valueList, types.StringValue(str)) + } + // Ensure the list is of type types.StringType + return types.ListValueMust(types.StringType, valueList) } diff --git a/internal/provider/integration_ms365_resource.go b/internal/provider/integration_ms365_resource.go index 85d7b6e..cdec3b5 100644 --- a/internal/provider/integration_ms365_resource.go +++ b/internal/provider/integration_ms365_resource.go @@ -3,9 +3,9 @@ package provider 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" @@ -224,5 +224,23 @@ func (r *integrationMs365Resource) Delete(ctx context.Context, req resource.Dele } func (r *integrationMs365Resource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { - resource.ImportStatePassthroughID(ctx, path.Root("mrn"), req, resp) + mrn := req.ID + integration, err := r.client.GetClientIntegration(ctx, mrn) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get Ms365 integration, got error: %s", err)) + return + } + + model := integrationMs365ResourceModel{ + Mrn: types.StringValue(integration.Mrn), + Name: types.StringValue(integration.Name), + SpaceId: types.StringValue(strings.Split(integration.Mrn, "/")[len(strings.Split(integration.Mrn, "/"))-3]), + TenantId: types.StringValue(integration.ConfigurationOptions.Ms365ConfigurationOptions.TenantId), + ClientId: types.StringValue(integration.ConfigurationOptions.Ms365ConfigurationOptions.ClientId), + Credential: integrationMs365CredentialModel{ + PEMFile: types.StringPointerValue(nil), + }, + } + + resp.State.Set(ctx, &model) } diff --git a/internal/provider/integration_slack_resource.go b/internal/provider/integration_slack_resource.go index 833b59e..59c13bd 100644 --- a/internal/provider/integration_slack_resource.go +++ b/internal/provider/integration_slack_resource.go @@ -4,9 +4,9 @@ import ( "context" "fmt" "regexp" + "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" @@ -209,5 +209,19 @@ func (r *integrationSlackResource) Delete(ctx context.Context, req resource.Dele } func (r *integrationSlackResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { - resource.ImportStatePassthroughID(ctx, path.Root("mrn"), req, resp) + mrn := req.ID + integration, err := r.client.GetClientIntegration(ctx, mrn) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get Slack integration, got error: %s", err)) + return + } + + model := integrationSlackResourceModel{ + Mrn: types.StringValue(integration.Mrn), + Name: types.StringValue(integration.Name), + SlackToken: types.StringPointerValue(nil), + SpaceId: types.StringValue(strings.Split(integration.Mrn, "/")[len(strings.Split(integration.Mrn, "/"))-3]), + } + + resp.State.Set(ctx, &model) } diff --git a/internal/provider/space_resource.go b/internal/provider/space_resource.go index c3ee9f9..e78cb4c 100644 --- a/internal/provider/space_resource.go +++ b/internal/provider/space_resource.go @@ -236,8 +236,5 @@ func (r *SpaceResource) ImportState(ctx context.Context, req resource.ImportStat OrgID: types.StringValue(spacePayload.Organization.Id), } - resp.State.SetAttribute(ctx, path.Root("id"), model.SpaceID) - resp.State.SetAttribute(ctx, path.Root("name"), model.Name) - resp.State.SetAttribute(ctx, path.Root("org_id"), model.OrgID) - resp.State.SetAttribute(ctx, path.Root("mrn"), model.SpaceMrn) + resp.State.Set(ctx, &model) }