Skip to content

Commit

Permalink
Allow to user change blueprint identifier on entity resource
Browse files Browse the repository at this point in the history
  • Loading branch information
dvirsegev committed Oct 23, 2023
1 parent 62a7df7 commit 7b5b605
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 5 deletions.
15 changes: 13 additions & 2 deletions port/entity/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,28 @@ func (r *EntityResource) Update(ctx context.Context, req resource.UpdateRequest,

var en *cli.Entity

if previousState.Identifier.IsNull() {
isBlueprintChanged := !previousState.Blueprint.IsNull() && previousState.Blueprint.ValueString() != state.Blueprint.ValueString()

if previousState.Identifier.IsNull() || isBlueprintChanged {
en, err = r.portClient.CreateEntity(ctx, e, runID)
} else {
en, err = r.portClient.UpdateEntity(ctx, previousState.Identifier.ValueString(), previousState.Blueprint.ValueString(), e, runID)
}

if err != nil {
resp.Diagnostics.AddError("failed to create entity", err.Error())
resp.Diagnostics.AddError("failed to update entity", err.Error())
return
}

if isBlueprintChanged {
// Delete the old entity
err := r.portClient.DeleteEntity(ctx, previousState.Identifier.ValueString(), previousState.Blueprint.ValueString())
if err != nil {
resp.Diagnostics.AddError("failed to delete entity", err.Error())
return
}
}

writeEntityComputedFieldsToState(state, en)

resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
Expand Down
96 changes: 96 additions & 0 deletions port/entity/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,3 +499,99 @@ func TestAccPortEntityUpdateIdentifier(t *testing.T) {
})

}

func TestAccPortEntityUpdateBlueprintIdentifier(t *testing.T) {

blueprintIdentifier := utils.GenID()
blueprintIdentifier2 := utils.GenID()
entityIdentifier := utils.GenID()

var testAccActionConfigCreate = fmt.Sprintf(`
resource "port_blueprint" "microservice" {
title = "TF Provider Test BP0"
icon = "Terraform"
identifier = "%s"
properties = {
"string_props" = {
"myStringIdentifier" = {
"title" = "My String Identifier"
}
}
}
}
resource "port_entity" "microservice" {
title = "TF Provider Test Entity0"
blueprint = port_blueprint.microservice.identifier
identifier = "%s"
properties = {
"string_props" = {
"myStringIdentifier" = "My String Value"
}
}
}`, blueprintIdentifier, entityIdentifier)

var testAccActionConfigUpdate = fmt.Sprintf(`
resource "port_blueprint" "microservice" {
title = "TF Provider Test BP0"
icon = "Terraform"
identifier = "%s"
properties = {
"string_props" = {
"myStringIdentifier" = {
"title" = "My String Identifier"
}
}
}
}
resource "port_blueprint" "microservice2" {
title = "TF Provider Test BP0"
icon = "Terraform"
identifier = "%s"
properties = {
"string_props" = {
"myStringIdentifier" = {
"title" = "My String Identifier"
}
}
}
}
resource "port_entity" "microservice" {
title = "TF Provider Test Entity0"
blueprint = port_blueprint.microservice2.identifier
identifier = "%s"
properties = {
"string_props" = {
"myStringIdentifier" = "My String Value2"
}
}
}`, blueprintIdentifier, blueprintIdentifier2, entityIdentifier)

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,

Steps: []resource.TestStep{
{
Config: acctest.ProviderConfig + testAccActionConfigCreate,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("port_entity.microservice", "identifier", entityIdentifier),
resource.TestCheckResourceAttr("port_entity.microservice", "title", "TF Provider Test Entity0"),
resource.TestCheckResourceAttr("port_entity.microservice", "blueprint", blueprintIdentifier),
resource.TestCheckResourceAttr("port_entity.microservice", "properties.string_props.myStringIdentifier", "My String Value"),
),
},
{
Config: acctest.ProviderConfig + testAccActionConfigUpdate,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("port_entity.microservice", "identifier", entityIdentifier),
resource.TestCheckResourceAttr("port_entity.microservice", "title", "TF Provider Test Entity0"),
resource.TestCheckResourceAttr("port_entity.microservice", "blueprint", blueprintIdentifier2),
resource.TestCheckResourceAttr("port_entity.microservice", "properties.string_props.myStringIdentifier", "My String Value2"),
),
},
},
})

}
3 changes: 0 additions & 3 deletions port/entity/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ func EntitySchema() map[string]schema.Attribute {
"created_at": schema.StringAttribute{
MarkdownDescription: "The creation date of the entity",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"created_by": schema.StringAttribute{
MarkdownDescription: "The creator of the entity",
Expand Down

0 comments on commit 7b5b605

Please sign in to comment.