Skip to content

Commit

Permalink
feat: added read functionality and updated integration
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Theuermann <[email protected]>
  • Loading branch information
mati007thm committed Dec 18, 2024
1 parent 4778115 commit d8bb3ca
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 23 deletions.
3 changes: 2 additions & 1 deletion docs/resources/integration_gcp.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ provider "mondoo" {
}
# Setup the GCP integration
resource "mondoo_integration_gcp" "name" {
resource "mondoo_integration_gcp" "gcp_integration" {
name = "GCP ${data.google_project.project.name}"
project_id = data.google_project.project.project_id
credentials = {
Expand All @@ -78,6 +78,7 @@ resource "mondoo_integration_gcp" "name" {

### Optional

- `organization_id` (String) GCP organization id
- `project_id` (String) GCP project id
- `space_id` (String) Mondoo Space Identifier. If it is not provided, the provider space is used.

Expand Down
2 changes: 1 addition & 1 deletion examples/resources/mondoo_integration_gcp/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ provider "mondoo" {
}

# Setup the GCP integration
resource "mondoo_integration_gcp" "name" {
resource "mondoo_integration_gcp" "gcp_integration" {
name = "GCP ${data.google_project.project.name}"
project_id = data.google_project.project.project_id
credentials = {
Expand Down
5 changes: 3 additions & 2 deletions internal/provider/gql.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,9 @@ type HostedAwsConfigurationOptions struct {
}

type GcpConfigurationOptions struct {
ProjectId string
DiscoverAll bool
ProjectId string
OrganizationId string
DiscoverAll bool
}

type ShodanConfigurationOptions struct {
Expand Down
74 changes: 55 additions & 19 deletions internal/provider/integration_gcp_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"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/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 All @@ -33,9 +35,10 @@ type integrationGcpResourceModel struct {
SpaceID types.String `tfsdk:"space_id"`

// integration details
Mrn types.String `tfsdk:"mrn"`
Name types.String `tfsdk:"name"`
ProjectID types.String `tfsdk:"project_id"`
Mrn types.String `tfsdk:"mrn"`
Name types.String `tfsdk:"name"`
ProjectID types.String `tfsdk:"project_id"`
OrganizationID types.String `tfsdk:"organization_id"`

// credentials
Credential integrationGcpCredentialModel `tfsdk:"credentials"`
Expand All @@ -45,6 +48,17 @@ type integrationGcpCredentialModel struct {
PrivateKey types.String `tfsdk:"private_key"`
}

func (m integrationGcpResourceModel) GetConfigurationOptions() *mondoov1.GcpConfigurationOptionsInput {
opts := &mondoov1.GcpConfigurationOptionsInput{
ProjectID: mondoov1.NewStringPtr(mondoov1.String(m.ProjectID.ValueString())),
OrganizationID: mondoov1.NewStringPtr(mondoov1.String(m.OrganizationID.ValueString())),
ServiceAccount: mondoov1.NewStringPtr(mondoov1.String(m.Credential.PrivateKey.ValueString())),
DiscoverAll: mondoov1.NewBooleanPtr(mondoov1.Boolean(true)),
}

return opts
}

func (r *integrationGcpResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_integration_gcp"
}
Expand Down Expand Up @@ -78,6 +92,20 @@ func (r *integrationGcpResource) Schema(ctx context.Context, req resource.Schema
"project_id": schema.StringAttribute{
MarkdownDescription: "GCP project id",
Optional: true,
Computed: true,
Default: stringdefault.StaticString(""),
Validators: []validator.String{
stringvalidator.ExactlyOneOf(path.MatchRoot("project_id"), path.MatchRoot("organization_id")),
},
},
"organization_id": schema.StringAttribute{
MarkdownDescription: "GCP organization id",
Optional: true,
Computed: true,
Default: stringdefault.StaticString(""),
Validators: []validator.String{
stringvalidator.ExactlyOneOf(path.MatchRoot("project_id"), path.MatchRoot("organization_id")),
},
},
"credentials": schema.SingleNestedAttribute{
Required: true,
Expand Down Expand Up @@ -137,11 +165,7 @@ func (r *integrationGcpResource) Create(ctx context.Context, req resource.Create
data.Name.ValueString(),
mondoov1.ClientIntegrationTypeGcp,
mondoov1.ClientIntegrationConfigurationInput{
GcpConfigurationOptions: &mondoov1.GcpConfigurationOptionsInput{
ProjectID: mondoov1.NewStringPtr(mondoov1.String(data.ProjectID.ValueString())),
ServiceAccount: mondoov1.NewStringPtr(mondoov1.String(data.Credential.PrivateKey.ValueString())),
DiscoverAll: mondoov1.NewBooleanPtr(mondoov1.Boolean(true)),
},
GcpConfigurationOptions: data.GetConfigurationOptions(),
})
if err != nil {
resp.Diagnostics.
Expand All @@ -159,7 +183,6 @@ func (r *integrationGcpResource) Create(ctx context.Context, req resource.Create
AddWarning("Client Error",
fmt.Sprintf("Unable to trigger integration, got error: %s", err),
)
return
}

// Save space mrn into the Terraform state.
Expand All @@ -182,9 +205,25 @@ func (r *integrationGcpResource) Read(ctx context.Context, req resource.ReadRequ
}

// Read API call logic
integration, err := r.client.GetClientIntegration(ctx, data.Mrn.ValueString())
if err != nil {
resp.State.RemoveResource(ctx)
return
}

model := integrationGcpResourceModel{
Mrn: types.StringValue(integration.Mrn),
Name: types.StringValue(integration.Name),
SpaceID: types.StringValue(integration.SpaceID()),
ProjectID: types.StringValue(integration.ConfigurationOptions.GcpConfigurationOptions.ProjectId),
OrganizationID: types.StringValue(integration.ConfigurationOptions.GcpConfigurationOptions.OrganizationId),
Credential: integrationGcpCredentialModel{
PrivateKey: types.StringValue(data.Credential.PrivateKey.ValueString()),
},
}

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

func (r *integrationGcpResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
Expand All @@ -199,11 +238,7 @@ func (r *integrationGcpResource) Update(ctx context.Context, req resource.Update

// Do GraphQL request to API to update the resource.
opts := mondoov1.ClientIntegrationConfigurationInput{
GcpConfigurationOptions: &mondoov1.GcpConfigurationOptionsInput{
ProjectID: mondoov1.NewStringPtr(mondoov1.String(data.ProjectID.ValueString())),
ServiceAccount: mondoov1.NewStringPtr(mondoov1.String(data.Credential.PrivateKey.ValueString())),
DiscoverAll: mondoov1.NewBooleanPtr(mondoov1.Boolean(true)),
},
GcpConfigurationOptions: data.GetConfigurationOptions(),
}

_, err := r.client.UpdateIntegration(ctx,
Expand Down Expand Up @@ -253,10 +288,11 @@ func (r *integrationGcpResource) ImportState(ctx context.Context, req resource.I
}

model := integrationGcpResourceModel{
Mrn: types.StringValue(integration.Mrn),
Name: types.StringValue(integration.Name),
SpaceID: types.StringValue(integration.SpaceID()),
ProjectID: types.StringValue(integration.ConfigurationOptions.GcpConfigurationOptions.ProjectId),
Mrn: types.StringValue(integration.Mrn),
Name: types.StringValue(integration.Name),
SpaceID: types.StringValue(integration.SpaceID()),
ProjectID: types.StringValue(integration.ConfigurationOptions.GcpConfigurationOptions.ProjectId),
OrganizationID: types.StringValue(integration.ConfigurationOptions.GcpConfigurationOptions.OrganizationId),
Credential: integrationGcpCredentialModel{
PrivateKey: types.StringPointerValue(nil),
},
Expand Down

0 comments on commit d8bb3ca

Please sign in to comment.