Skip to content

Commit

Permalink
If blueprint identifier isn't exists, remove it from the terrform sta…
Browse files Browse the repository at this point in the history
…te (#36)
  • Loading branch information
dvirsegev authored Apr 17, 2023
1 parent 1120d12 commit 4251f59
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 18 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.vscode/
.env
__debug_bin
local/
__debug_bin
terraform-provider-port-labs
8 changes: 4 additions & 4 deletions port/cli/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
)

func (c *PortClient) ReadAction(ctx context.Context, blueprintID, id string) (*Action, error) {
func (c *PortClient) ReadAction(ctx context.Context, blueprintID, id string) (*Action, int, error) {
pb := &PortBody{}
url := "v1/blueprints/{blueprint_identifier}/actions/{action_identifier}"
resp, err := c.Client.R().
Expand All @@ -17,12 +17,12 @@ func (c *PortClient) ReadAction(ctx context.Context, blueprintID, id string) (*A
SetPathParam("action_identifier", id).
Get(url)
if err != nil {
return nil, err
return nil, resp.StatusCode(), err
}
if !pb.OK {
return nil, fmt.Errorf("failed to read action, got: %s", resp.Body())
return nil, resp.StatusCode(), fmt.Errorf("failed to read action, got: %s", resp.Body())
}
return &pb.Action, nil
return &pb.Action, resp.StatusCode(), nil
}

func (c *PortClient) CreateAction(ctx context.Context, blueprintID string, action *Action) (*Action, error) {
Expand Down
8 changes: 4 additions & 4 deletions port/cli/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
)

func (c *PortClient) ReadBlueprint(ctx context.Context, id string) (*Blueprint, error) {
func (c *PortClient) ReadBlueprint(ctx context.Context, id string) (*Blueprint, int, error) {
pb := &PortBody{}
url := "v1/blueprints/{identifier}"
resp, err := c.Client.R().
Expand All @@ -17,12 +17,12 @@ func (c *PortClient) ReadBlueprint(ctx context.Context, id string) (*Blueprint,
SetPathParam("identifier", id).
Get(url)
if err != nil {
return nil, err
return nil, resp.StatusCode(), err
}
if !pb.OK {
return nil, fmt.Errorf("failed to read blueprint, got: %s", resp.Body())
return nil, resp.StatusCode(), fmt.Errorf("failed to read blueprint, got: %s", resp.Body())
}
return &pb.Blueprint, nil
return &pb.Blueprint, resp.StatusCode(), nil
}

func (c *PortClient) CreateBlueprint(ctx context.Context, b *Blueprint) (*Blueprint, error) {
Expand Down
10 changes: 5 additions & 5 deletions port/cli/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
)

func (c *PortClient) ReadEntity(ctx context.Context, id string, blueprint string) (*Entity, error) {
func (c *PortClient) ReadEntity(ctx context.Context, id string, blueprint string) (*Entity, int, error) {
url := "v1/blueprints/{blueprint}/entities/{identifier}"
resp, err := c.Client.R().
SetHeader("Accept", "application/json").
Expand All @@ -15,17 +15,17 @@ func (c *PortClient) ReadEntity(ctx context.Context, id string, blueprint string
SetPathParam("identifier", id).
Get(url)
if err != nil {
return nil, err
return nil, resp.StatusCode(), err
}
var pb PortBody
err = json.Unmarshal(resp.Body(), &pb)
if err != nil {
return nil, err
return nil, resp.StatusCode(), err
}
if !pb.OK {
return nil, fmt.Errorf("failed to read entity, got: %s", resp.Body())
return nil, resp.StatusCode(), fmt.Errorf("failed to read entity, got: %s", resp.Body())
}
return &pb.Entity, nil
return &pb.Entity, resp.StatusCode(), nil
}

func (c *PortClient) CreateEntity(ctx context.Context, e *Entity, runID string) (*Entity, error) {
Expand Down
7 changes: 6 additions & 1 deletion port/resource_port_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,13 @@ func newActionResource() *schema.Resource {
func readAction(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
c := m.(*cli.PortClient)
action, err := c.ReadAction(ctx, d.Get("blueprint_identifier").(string), d.Id())
action, statusCode, err := c.ReadAction(ctx, d.Get("blueprint_identifier").(string), d.Id())
if err != nil {
if statusCode == 404 {
d.SetId("")
return diags
}

return diag.FromErr(err)
}
writeActionFieldsToResource(d, action)
Expand Down
8 changes: 7 additions & 1 deletion port/resource_port_blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,16 @@ func newBlueprintResource() *schema.Resource {
func readBlueprint(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
c := m.(*cli.PortClient)
b, err := c.ReadBlueprint(ctx, d.Id())
b, statusCode, err := c.ReadBlueprint(ctx, d.Id())
if err != nil {
if statusCode == 404 {
d.SetId("")
return diags
}

return diag.FromErr(err)
}

writeBlueprintFieldsToResource(d, b)

return diags
Expand Down
9 changes: 7 additions & 2 deletions port/resource_port_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func writeEntityFieldsToResource(d *schema.ResourceData, e *cli.Entity) {
func createEntity(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
c := m.(*cli.PortClient)
bp, err := c.ReadBlueprint(ctx, d.Get("blueprint").(string))
bp, _, err := c.ReadBlueprint(ctx, d.Get("blueprint").(string))
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -297,8 +297,13 @@ func createEntity(ctx context.Context, d *schema.ResourceData, m interface{}) di
func readEntity(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
c := m.(*cli.PortClient)
e, err := c.ReadEntity(ctx, d.Id(), d.Get("blueprint").(string))
e, statusCode, err := c.ReadEntity(ctx, d.Id(), d.Get("blueprint").(string))
if err != nil {
if statusCode == 404 {
d.SetId("")
return diags
}

return diag.FromErr(err)
}
writeEntityFieldsToResource(d, e)
Expand Down

0 comments on commit 4251f59

Please sign in to comment.