Skip to content

Commit 4251f59

Browse files
authored
If blueprint identifier isn't exists, remove it from the terrform state (#36)
1 parent 1120d12 commit 4251f59

7 files changed

+36
-18
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
.vscode/
22
.env
3-
__debug_bin
3+
local/
4+
__debug_bin
5+
terraform-provider-port-labs

port/cli/action.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"fmt"
77
)
88

9-
func (c *PortClient) ReadAction(ctx context.Context, blueprintID, id string) (*Action, error) {
9+
func (c *PortClient) ReadAction(ctx context.Context, blueprintID, id string) (*Action, int, error) {
1010
pb := &PortBody{}
1111
url := "v1/blueprints/{blueprint_identifier}/actions/{action_identifier}"
1212
resp, err := c.Client.R().
@@ -17,12 +17,12 @@ func (c *PortClient) ReadAction(ctx context.Context, blueprintID, id string) (*A
1717
SetPathParam("action_identifier", id).
1818
Get(url)
1919
if err != nil {
20-
return nil, err
20+
return nil, resp.StatusCode(), err
2121
}
2222
if !pb.OK {
23-
return nil, fmt.Errorf("failed to read action, got: %s", resp.Body())
23+
return nil, resp.StatusCode(), fmt.Errorf("failed to read action, got: %s", resp.Body())
2424
}
25-
return &pb.Action, nil
25+
return &pb.Action, resp.StatusCode(), nil
2626
}
2727

2828
func (c *PortClient) CreateAction(ctx context.Context, blueprintID string, action *Action) (*Action, error) {

port/cli/blueprint.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"fmt"
77
)
88

9-
func (c *PortClient) ReadBlueprint(ctx context.Context, id string) (*Blueprint, error) {
9+
func (c *PortClient) ReadBlueprint(ctx context.Context, id string) (*Blueprint, int, error) {
1010
pb := &PortBody{}
1111
url := "v1/blueprints/{identifier}"
1212
resp, err := c.Client.R().
@@ -17,12 +17,12 @@ func (c *PortClient) ReadBlueprint(ctx context.Context, id string) (*Blueprint,
1717
SetPathParam("identifier", id).
1818
Get(url)
1919
if err != nil {
20-
return nil, err
20+
return nil, resp.StatusCode(), err
2121
}
2222
if !pb.OK {
23-
return nil, fmt.Errorf("failed to read blueprint, got: %s", resp.Body())
23+
return nil, resp.StatusCode(), fmt.Errorf("failed to read blueprint, got: %s", resp.Body())
2424
}
25-
return &pb.Blueprint, nil
25+
return &pb.Blueprint, resp.StatusCode(), nil
2626
}
2727

2828
func (c *PortClient) CreateBlueprint(ctx context.Context, b *Blueprint) (*Blueprint, error) {

port/cli/entity.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"fmt"
77
)
88

9-
func (c *PortClient) ReadEntity(ctx context.Context, id string, blueprint string) (*Entity, error) {
9+
func (c *PortClient) ReadEntity(ctx context.Context, id string, blueprint string) (*Entity, int, error) {
1010
url := "v1/blueprints/{blueprint}/entities/{identifier}"
1111
resp, err := c.Client.R().
1212
SetHeader("Accept", "application/json").
@@ -15,17 +15,17 @@ func (c *PortClient) ReadEntity(ctx context.Context, id string, blueprint string
1515
SetPathParam("identifier", id).
1616
Get(url)
1717
if err != nil {
18-
return nil, err
18+
return nil, resp.StatusCode(), err
1919
}
2020
var pb PortBody
2121
err = json.Unmarshal(resp.Body(), &pb)
2222
if err != nil {
23-
return nil, err
23+
return nil, resp.StatusCode(), err
2424
}
2525
if !pb.OK {
26-
return nil, fmt.Errorf("failed to read entity, got: %s", resp.Body())
26+
return nil, resp.StatusCode(), fmt.Errorf("failed to read entity, got: %s", resp.Body())
2727
}
28-
return &pb.Entity, nil
28+
return &pb.Entity, resp.StatusCode(), nil
2929
}
3030

3131
func (c *PortClient) CreateEntity(ctx context.Context, e *Entity, runID string) (*Entity, error) {

port/resource_port_action.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,13 @@ func newActionResource() *schema.Resource {
212212
func readAction(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
213213
var diags diag.Diagnostics
214214
c := m.(*cli.PortClient)
215-
action, err := c.ReadAction(ctx, d.Get("blueprint_identifier").(string), d.Id())
215+
action, statusCode, err := c.ReadAction(ctx, d.Get("blueprint_identifier").(string), d.Id())
216216
if err != nil {
217+
if statusCode == 404 {
218+
d.SetId("")
219+
return diags
220+
}
221+
217222
return diag.FromErr(err)
218223
}
219224
writeActionFieldsToResource(d, action)

port/resource_port_blueprint.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,16 @@ func newBlueprintResource() *schema.Resource {
291291
func readBlueprint(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
292292
var diags diag.Diagnostics
293293
c := m.(*cli.PortClient)
294-
b, err := c.ReadBlueprint(ctx, d.Id())
294+
b, statusCode, err := c.ReadBlueprint(ctx, d.Id())
295295
if err != nil {
296+
if statusCode == 404 {
297+
d.SetId("")
298+
return diags
299+
}
300+
296301
return diag.FromErr(err)
297302
}
303+
298304
writeBlueprintFieldsToResource(d, b)
299305

300306
return diags

port/resource_port_entity.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ func writeEntityFieldsToResource(d *schema.ResourceData, e *cli.Entity) {
274274
func createEntity(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
275275
var diags diag.Diagnostics
276276
c := m.(*cli.PortClient)
277-
bp, err := c.ReadBlueprint(ctx, d.Get("blueprint").(string))
277+
bp, _, err := c.ReadBlueprint(ctx, d.Get("blueprint").(string))
278278
if err != nil {
279279
return diag.FromErr(err)
280280
}
@@ -297,8 +297,13 @@ func createEntity(ctx context.Context, d *schema.ResourceData, m interface{}) di
297297
func readEntity(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
298298
var diags diag.Diagnostics
299299
c := m.(*cli.PortClient)
300-
e, err := c.ReadEntity(ctx, d.Id(), d.Get("blueprint").(string))
300+
e, statusCode, err := c.ReadEntity(ctx, d.Id(), d.Get("blueprint").(string))
301301
if err != nil {
302+
if statusCode == 404 {
303+
d.SetId("")
304+
return diags
305+
}
306+
302307
return diag.FromErr(err)
303308
}
304309
writeEntityFieldsToResource(d, e)

0 commit comments

Comments
 (0)