Skip to content

Commit 221c94f

Browse files
authored
Port 3160 add import support (#45)
1 parent 7672a6b commit 221c94f

6 files changed

+267
-18
lines changed

port/resource_port_action.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package port
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"strconv"
8+
"strings"
79

810
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
911
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -211,13 +213,29 @@ func newActionResource() *schema.Resource {
211213
Description: "Whether the action requires approval or not",
212214
},
213215
},
216+
Importer: &schema.ResourceImporter{
217+
StateContext: schema.ImportStatePassthroughContext,
218+
},
214219
}
215220
}
216221

217222
func readAction(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
218223
var diags diag.Diagnostics
219224
c := m.(*cli.PortClient)
220-
action, statusCode, err := c.ReadAction(ctx, d.Get("blueprint_identifier").(string), d.Id())
225+
id := d.Id()
226+
actionIdentifier := id
227+
blueprintIdentifier := d.Get("blueprint_identifier").(string)
228+
if strings.Contains(id, ":") {
229+
parts := strings.SplitN(id, ":", 2)
230+
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
231+
return diag.FromErr(fmt.Errorf("unexpected format of ID (%s), expected blueprintId:entityId", id))
232+
}
233+
234+
blueprintIdentifier = parts[0]
235+
actionIdentifier = parts[1]
236+
}
237+
238+
action, statusCode, err := c.ReadAction(ctx, blueprintIdentifier, actionIdentifier)
221239
if err != nil {
222240
if statusCode == 404 {
223241
d.SetId("")
@@ -226,12 +244,14 @@ func readAction(ctx context.Context, d *schema.ResourceData, m interface{}) diag
226244

227245
return diag.FromErr(err)
228246
}
229-
writeActionFieldsToResource(d, action)
247+
writeActionFieldsToResource(d, action, blueprintIdentifier)
230248
return diags
231249
}
232250

233-
func writeActionFieldsToResource(d *schema.ResourceData, action *cli.Action) {
251+
func writeActionFieldsToResource(d *schema.ResourceData, action *cli.Action, blueprintIdentifier string) {
234252
d.SetId(action.Identifier)
253+
d.Set("blueprint_identifier", blueprintIdentifier)
254+
d.Set("identifier", action.Identifier)
235255
d.Set("title", action.Title)
236256
d.Set("icon", action.Icon)
237257
d.Set("description", action.Description)
@@ -426,7 +446,11 @@ func actionResourceToBody(d *schema.ResourceData) (*cli.Action, error) {
426446
func deleteAction(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
427447
var diags diag.Diagnostics
428448
c := m.(*cli.PortClient)
429-
err := c.DeleteAction(ctx, d.Get("blueprint_identifier").(string), d.Id())
449+
id := d.Id()
450+
actionIdentifier := id
451+
blueprintIdentifier := d.Get("blueprint_identifier").(string)
452+
453+
err := c.DeleteAction(ctx, blueprintIdentifier, actionIdentifier)
430454
if err != nil {
431455
return diag.FromErr(err)
432456
}
@@ -440,15 +464,21 @@ func createAction(ctx context.Context, d *schema.ResourceData, m interface{}) di
440464
if err != nil {
441465
return diag.FromErr(err)
442466
}
467+
443468
var a *cli.Action
469+
470+
blueprintIdentifier := d.Get("blueprint_identifier").(string)
471+
actionIdentifier := d.Id()
444472
if d.Id() != "" {
445-
a, err = c.UpdateAction(ctx, d.Get("blueprint_identifier").(string), d.Id(), action)
473+
a, err = c.UpdateAction(ctx, blueprintIdentifier, actionIdentifier, action)
446474
} else {
447475
a, err = c.CreateAction(ctx, d.Get("blueprint_identifier").(string), action)
448476
}
477+
449478
if err != nil {
450479
return diag.FromErr(err)
451480
}
481+
452482
d.SetId(a.Identifier)
453483
return diags
454484
}

port/resource_port_action_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,82 @@ func TestAccPortAction(t *testing.T) {
152152
})
153153
}
154154

155+
func TestActionImport(t *testing.T) {
156+
blueprintIdentifier := genID()
157+
actionIdentifier := genID()
158+
var testAccActionConfigCreate = fmt.Sprintf(`
159+
resource "port-labs_blueprint" "microservice" {
160+
title = "TF test microservice"
161+
icon = "Terraform"
162+
identifier = "%s"
163+
properties {
164+
identifier = "text"
165+
type = "string"
166+
title = "text"
167+
}
168+
}
169+
resource "port-labs_action" "restart_microservice" {
170+
title = "Restart service"
171+
icon = "Terraform"
172+
identifier = "%s"
173+
blueprint_identifier = port-labs_blueprint.microservice.identifier
174+
trigger = "DAY-2"
175+
invocation_method {
176+
type = "KAFKA"
177+
}
178+
user_properties {
179+
identifier = "reason"
180+
type = "string"
181+
title = "Reason"
182+
default = "test"
183+
}
184+
user_properties {
185+
identifier = "delay"
186+
type = "number"
187+
title = "Delay"
188+
default = 3
189+
}
190+
user_properties {
191+
identifier = "clear_cache"
192+
type = "boolean"
193+
title = "Clear cache"
194+
default = true
195+
}
196+
user_properties {
197+
identifier = "services"
198+
type = "array"
199+
title = "Services"
200+
default_items = ["api", "frontend"]
201+
}
202+
user_properties {
203+
identifier = "config"
204+
type = "object"
205+
title = "Config"
206+
default = jsonencode({"when":"immediate"})
207+
}
208+
}
209+
`, blueprintIdentifier, actionIdentifier)
210+
resource.Test(t, resource.TestCase{
211+
Providers: map[string]*schema.Provider{
212+
"port-labs": Provider(),
213+
},
214+
Steps: []resource.TestStep{
215+
{
216+
Config: testAccActionConfigCreate,
217+
Check: resource.ComposeTestCheckFunc(
218+
resource.TestCheckResourceAttr("port-labs_action.restart_microservice", "title", "Restart service"),
219+
),
220+
},
221+
{
222+
ResourceName: "port-labs_action.restart_microservice",
223+
ImportState: true,
224+
ImportStateVerify: true,
225+
ImportStateId: fmt.Sprintf("%s:%s", blueprintIdentifier, actionIdentifier),
226+
},
227+
},
228+
})
229+
}
230+
155231
func TestAccPortActionPropMeta(t *testing.T) {
156232
identifier := genID()
157233
actionIdentifier := genID()

port/resource_port_blueprint.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,11 @@ func newBlueprintResource() *schema.Resource {
342342
Computed: true,
343343
},
344344
},
345+
Importer: &schema.ResourceImporter{
346+
StateContext: schema.ImportStatePassthroughContext,
347+
},
345348
}
349+
346350
}
347351

348352
func readBlueprint(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
@@ -412,6 +416,7 @@ func writeBlueprintFieldsToResource(d *schema.ResourceData, b *cli.Blueprint) {
412416
d.SetId(b.Identifier)
413417
d.Set("title", b.Title)
414418
d.Set("icon", b.Icon)
419+
d.Set("identifier", b.Identifier)
415420
d.Set("description", b.Description)
416421
d.Set("created_at", b.CreatedAt.String())
417422
d.Set("created_by", b.CreatedBy)

port/resource_port_blueprint_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,41 @@ func TestAccPortBlueprint(t *testing.T) {
149149
})
150150
}
151151

152+
func TestAccPortBlueprintImport(t *testing.T) {
153+
var testAccActionConfigCreate = `
154+
resource "port-labs_blueprint" "microservice" {
155+
title = "microservice"
156+
icon = "Terraform"
157+
identifier = "import_microservice"
158+
properties {
159+
identifier = "bool"
160+
type = "boolean"
161+
title = "boolean"
162+
}
163+
}
164+
`
165+
resource.Test(t, resource.TestCase{
166+
Providers: map[string]*schema.Provider{
167+
"port-labs": Provider(),
168+
},
169+
Steps: []resource.TestStep{
170+
{
171+
Config: testAccActionConfigCreate,
172+
Check: resource.ComposeTestCheckFunc(
173+
resource.TestCheckResourceAttr("port-labs_blueprint.microservice", "title", "microservice"),
174+
resource.TestCheckResourceAttr("port-labs_blueprint.microservice", "identifier", "import_microservice"),
175+
),
176+
},
177+
{
178+
ResourceName: "port-labs_blueprint.microservice",
179+
ImportState: true,
180+
ImportStateVerify: true,
181+
ImportStateVerifyIgnore: []string{"data_source"},
182+
},
183+
},
184+
})
185+
}
186+
152187
func TestAccBlueprintWithDefaultValue(t *testing.T) {
153188
identifier := genID()
154189
var testAccActionConfigCreate = fmt.Sprintf(`

port/resource_port_entity.go

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"strconv"
8+
"strings"
89

910
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -133,13 +134,19 @@ func newEntityResource() *schema.Resource {
133134
Computed: true,
134135
},
135136
},
137+
Importer: &schema.ResourceImporter{
138+
StateContext: schema.ImportStatePassthroughContext,
139+
},
136140
}
137141
}
138142

139143
func deleteEntity(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
140144
var diags diag.Diagnostics
141145
c := m.(*cli.PortClient)
142-
err := c.DeleteEntity(ctx, d.Id(), d.Get("blueprint").(string))
146+
id := d.Id()
147+
entityIdentifier := id
148+
blueprintIdentifier := d.Get("blueprint").(string)
149+
err := c.DeleteEntity(ctx, entityIdentifier, blueprintIdentifier)
143150
if err != nil {
144151
return diag.FromErr(err)
145152
}
@@ -192,6 +199,7 @@ func entityResourceToBody(d *schema.ResourceData, bp *cli.Blueprint) (*cli.Entit
192199
if id != "" {
193200
e.Identifier = id
194201
}
202+
195203
e.Title = d.Get("title").(string)
196204
e.Blueprint = d.Get("blueprint").(string)
197205

@@ -250,20 +258,20 @@ func writeEntityComputedFieldsToResource(d *schema.ResourceData, e *cli.Entity)
250258
d.Set("updated_by", e.UpdatedBy)
251259
}
252260

253-
func writeEntityFieldsToResource(d *schema.ResourceData, e *cli.Entity) {
261+
func writeEntityFieldsToResource(d *schema.ResourceData, e *cli.Entity, blueprintIdentifier string) {
254262
d.SetId(e.Identifier)
255263
d.Set("title", e.Title)
264+
d.Set("blueprint", e.Blueprint)
256265

257-
team := d.Get("team")
266+
entityTeams := e.Team
267+
if len(entityTeams) > 0 {
268+
team := d.Get("team")
258269

259-
if team != "" {
260-
d.Set("team", e.Team[0])
261-
}
262-
263-
teams := d.Get("teams").(*schema.Set)
264-
265-
if len(teams.List()) > 0 {
266-
d.Set("teams", e.Team)
270+
if team != "" {
271+
d.Set("team", e.Team[0])
272+
} else {
273+
d.Set("teams", e.Team)
274+
}
267275
}
268276

269277
d.Set("created_at", e.CreatedAt.String())
@@ -351,7 +359,20 @@ func createEntity(ctx context.Context, d *schema.ResourceData, m interface{}) di
351359
func readEntity(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
352360
var diags diag.Diagnostics
353361
c := m.(*cli.PortClient)
354-
e, statusCode, err := c.ReadEntity(ctx, d.Id(), d.Get("blueprint").(string))
362+
id := d.Id()
363+
blueprintIdentifier := d.Get("blueprint").(string)
364+
entityIdentifier := d.Id()
365+
366+
if strings.Contains(id, ":") {
367+
parts := strings.SplitN(id, ":", 2)
368+
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
369+
return diag.FromErr(fmt.Errorf("unexpected format of ID (%s), expected blueprintId:entityId", id))
370+
}
371+
blueprintIdentifier = parts[0]
372+
entityIdentifier = parts[1]
373+
}
374+
375+
e, statusCode, err := c.ReadEntity(ctx, entityIdentifier, blueprintIdentifier)
355376
if err != nil {
356377
if statusCode == 404 {
357378
d.SetId("")
@@ -360,7 +381,7 @@ func readEntity(ctx context.Context, d *schema.ResourceData, m interface{}) diag
360381

361382
return diag.FromErr(err)
362383
}
363-
writeEntityFieldsToResource(d, e)
384+
writeEntityFieldsToResource(d, e, blueprintIdentifier)
364385
if err != nil {
365386
return diag.FromErr(err)
366387
}

0 commit comments

Comments
 (0)