Skip to content

Commit 78cbb77

Browse files
authored
Allow to user change blueprint identifier on entity resource (#88)
1 parent 62a7df7 commit 78cbb77

File tree

3 files changed

+109
-5
lines changed

3 files changed

+109
-5
lines changed

port/entity/resource.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,28 @@ func (r *EntityResource) Update(ctx context.Context, req resource.UpdateRequest,
143143

144144
var en *cli.Entity
145145

146-
if previousState.Identifier.IsNull() {
146+
isBlueprintChanged := !previousState.Blueprint.IsNull() && previousState.Blueprint.ValueString() != state.Blueprint.ValueString()
147+
148+
if previousState.Identifier.IsNull() || isBlueprintChanged {
147149
en, err = r.portClient.CreateEntity(ctx, e, runID)
148150
} else {
149151
en, err = r.portClient.UpdateEntity(ctx, previousState.Identifier.ValueString(), previousState.Blueprint.ValueString(), e, runID)
150152
}
151153

152154
if err != nil {
153-
resp.Diagnostics.AddError("failed to create entity", err.Error())
155+
resp.Diagnostics.AddError("failed to update entity", err.Error())
154156
return
155157
}
156158

159+
if isBlueprintChanged {
160+
// Delete the old entity
161+
err := r.portClient.DeleteEntity(ctx, previousState.Identifier.ValueString(), previousState.Blueprint.ValueString())
162+
if err != nil {
163+
resp.Diagnostics.AddError("failed to delete entity", err.Error())
164+
return
165+
}
166+
}
167+
157168
writeEntityComputedFieldsToState(state, en)
158169

159170
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)

port/entity/resource_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,3 +499,99 @@ func TestAccPortEntityUpdateIdentifier(t *testing.T) {
499499
})
500500

501501
}
502+
503+
func TestAccPortEntityUpdateBlueprintIdentifier(t *testing.T) {
504+
505+
blueprintIdentifier := utils.GenID()
506+
blueprintIdentifier2 := utils.GenID()
507+
entityIdentifier := utils.GenID()
508+
509+
var testAccActionConfigCreate = fmt.Sprintf(`
510+
resource "port_blueprint" "microservice" {
511+
title = "TF Provider Test BP0"
512+
icon = "Terraform"
513+
identifier = "%s"
514+
properties = {
515+
"string_props" = {
516+
"myStringIdentifier" = {
517+
"title" = "My String Identifier"
518+
}
519+
}
520+
}
521+
}
522+
resource "port_entity" "microservice" {
523+
title = "TF Provider Test Entity0"
524+
blueprint = port_blueprint.microservice.identifier
525+
identifier = "%s"
526+
properties = {
527+
"string_props" = {
528+
"myStringIdentifier" = "My String Value"
529+
}
530+
}
531+
}`, blueprintIdentifier, entityIdentifier)
532+
533+
var testAccActionConfigUpdate = fmt.Sprintf(`
534+
resource "port_blueprint" "microservice" {
535+
title = "TF Provider Test BP0"
536+
icon = "Terraform"
537+
identifier = "%s"
538+
properties = {
539+
"string_props" = {
540+
"myStringIdentifier" = {
541+
"title" = "My String Identifier"
542+
}
543+
}
544+
}
545+
}
546+
547+
resource "port_blueprint" "microservice2" {
548+
title = "TF Provider Test BP0"
549+
icon = "Terraform"
550+
identifier = "%s"
551+
properties = {
552+
"string_props" = {
553+
"myStringIdentifier" = {
554+
"title" = "My String Identifier"
555+
}
556+
}
557+
}
558+
}
559+
560+
resource "port_entity" "microservice" {
561+
title = "TF Provider Test Entity0"
562+
blueprint = port_blueprint.microservice2.identifier
563+
identifier = "%s"
564+
properties = {
565+
"string_props" = {
566+
"myStringIdentifier" = "My String Value2"
567+
}
568+
}
569+
}`, blueprintIdentifier, blueprintIdentifier2, entityIdentifier)
570+
571+
resource.Test(t, resource.TestCase{
572+
PreCheck: func() { acctest.TestAccPreCheck(t) },
573+
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
574+
575+
Steps: []resource.TestStep{
576+
{
577+
Config: acctest.ProviderConfig + testAccActionConfigCreate,
578+
Check: resource.ComposeTestCheckFunc(
579+
resource.TestCheckResourceAttr("port_entity.microservice", "identifier", entityIdentifier),
580+
resource.TestCheckResourceAttr("port_entity.microservice", "title", "TF Provider Test Entity0"),
581+
resource.TestCheckResourceAttr("port_entity.microservice", "blueprint", blueprintIdentifier),
582+
resource.TestCheckResourceAttr("port_entity.microservice", "properties.string_props.myStringIdentifier", "My String Value"),
583+
),
584+
},
585+
{
586+
Config: acctest.ProviderConfig + testAccActionConfigUpdate,
587+
Check: resource.ComposeTestCheckFunc(
588+
resource.TestCheckResourceAttr("port_entity.microservice", "identifier", entityIdentifier),
589+
resource.TestCheckResourceAttr("port_entity.microservice", "title", "TF Provider Test Entity0"),
590+
resource.TestCheckResourceAttr("port_entity.microservice", "blueprint", blueprintIdentifier2),
591+
resource.TestCheckResourceAttr("port_entity.microservice", "properties.string_props.myStringIdentifier", "My String Value2"),
592+
),
593+
},
594+
},
595+
})
596+
597+
}

port/entity/schema.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,6 @@ func EntitySchema() map[string]schema.Attribute {
111111
"created_at": schema.StringAttribute{
112112
MarkdownDescription: "The creation date of the entity",
113113
Computed: true,
114-
PlanModifiers: []planmodifier.String{
115-
stringplanmodifier.UseStateForUnknown(),
116-
},
117114
},
118115
"created_by": schema.StringAttribute{
119116
MarkdownDescription: "The creator of the entity",

0 commit comments

Comments
 (0)