Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
delores-hashicorp committed Apr 18, 2024
1 parent 0dc5076 commit d4e739e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
24 changes: 24 additions & 0 deletions examples/test/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
terraform {
required_providers {
hcp = {
source = "localhost/providers/hcp"
version = "~>0.0.1"
}
}
}

provider "hcp" {
project_id = "567f02be-08ef-4609-b928-702577572393"
}


resource "hcp_service_principal" "example" {
name = "example-sp"
}

resource "hcp_project_iam_binding" "example" {
principal_id = hcp_service_principal.example.resource_id
role = "roles/admin"
}


9 changes: 9 additions & 0 deletions internal/provider/iam/resource_service_principal.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
clients "github.com/hashicorp/terraform-provider-hcp/internal/clients"
"github.com/hashicorp/terraform-provider-hcp/internal/hcpvalidator"
"github.com/hashicorp/terraform-provider-hcp/internal/provider/modifiers"
)

var _ resource.Resource = &resourceServicePrincipal{}
var _ resource.ResourceWithConfigure = &resourceServicePrincipal{}
var _ resource.ResourceWithModifyPlan = &resourceServicePrincipal{}

func NewServicePrincipalResource() resource.Resource {
return &resourceServicePrincipal{}
}
Expand Down Expand Up @@ -105,6 +110,10 @@ func (r *resourceServicePrincipal) Configure(_ context.Context, req resource.Con
r.client = client
}

func (r *resourceServicePrincipal) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
modifiers.ModifyPlanForDefaultProjectChange(ctx, r.client.Config.ProjectID, req.State, req.Config, req.Plan, resp)
}

type ServicePrincipal struct {
ResourceID types.String `tfsdk:"resource_id"`
ResourceName types.String `tfsdk:"resource_name"`
Expand Down
19 changes: 19 additions & 0 deletions internal/provider/modifiers/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,22 @@ func ModifyPlanForDefaultProjectChange(ctx context.Context, providerDefaultProje
resp.RequiresReplace.Append(projectIDPath)
}
}

func ModifyPlanForDefaultParentChange(ctx context.Context, providerDefaultProject string, state tfsdk.State, configAttributes, planAttributes AttrGettable, resp *resource.ModifyPlanResponse) {
if state.Raw.IsNull() {
return
}

projectIDPath := path.Root("project_id")

var configProject, plannedProject types.String
resp.Diagnostics.Append(configAttributes.GetAttribute(ctx, projectIDPath, &configProject)...)
resp.Diagnostics.Append(planAttributes.GetAttribute(ctx, projectIDPath, &plannedProject)...)

if configProject.IsNull() && !plannedProject.IsNull() && providerDefaultProject != plannedProject.ValueString() {
// There is no project configured on the resource, yet the provider project is different from
// the planned project value. We must conclude that the provider default project changed.
resp.Plan.SetAttribute(ctx, projectIDPath, types.StringValue(providerDefaultProject))
resp.RequiresReplace.Append(projectIDPath)
}
}

0 comments on commit d4e739e

Please sign in to comment.