|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | +) |
| 8 | + |
| 9 | +func (c *PortClient) ReadAction(ctx context.Context, blueprintID, id string) (*Action, error) { |
| 10 | + pb := &PortBody{} |
| 11 | + url := "v1/blueprints/{blueprint_identifier}/actions/{action_identifier}" |
| 12 | + resp, err := c.Client.R(). |
| 13 | + SetContext(ctx). |
| 14 | + SetHeader("Accept", "application/json"). |
| 15 | + SetResult(pb). |
| 16 | + SetPathParam("blueprint_identifier", blueprintID). |
| 17 | + SetPathParam("action_identifier", id). |
| 18 | + Get(url) |
| 19 | + if err != nil { |
| 20 | + return nil, err |
| 21 | + } |
| 22 | + if !pb.OK { |
| 23 | + return nil, fmt.Errorf("failed to read action, got: %s", resp.Body()) |
| 24 | + } |
| 25 | + return &pb.Action, nil |
| 26 | +} |
| 27 | + |
| 28 | +func (c *PortClient) CreateAction(ctx context.Context, blueprintID string, action *Action) (*Action, error) { |
| 29 | + url := "v1/blueprints/{blueprint_identifier}/actions" |
| 30 | + resp, err := c.Client.R(). |
| 31 | + SetBody(action). |
| 32 | + SetPathParam("blueprint_identifier", blueprintID). |
| 33 | + SetContext(ctx). |
| 34 | + Post(url) |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + var pb PortBody |
| 39 | + err = json.Unmarshal(resp.Body(), &pb) |
| 40 | + if err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + if !pb.OK { |
| 44 | + return nil, fmt.Errorf("failed to create action, got: %s", resp.Body()) |
| 45 | + } |
| 46 | + return &pb.Action, nil |
| 47 | +} |
| 48 | + |
| 49 | +func (c *PortClient) UpdateAction(ctx context.Context, blueprintID, actionID string, action *Action) (*Action, error) { |
| 50 | + url := "v1/blueprints/{blueprint_identifier}/actions/{action_identifier}" |
| 51 | + resp, err := c.Client.R(). |
| 52 | + SetBody(action). |
| 53 | + SetContext(ctx). |
| 54 | + SetPathParam("blueprint_identifier", blueprintID). |
| 55 | + SetPathParam("action_identifier", actionID). |
| 56 | + Put(url) |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + var pb PortBody |
| 61 | + err = json.Unmarshal(resp.Body(), &pb) |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + if !pb.OK { |
| 66 | + return nil, fmt.Errorf("failed to create action, got: %s", resp.Body()) |
| 67 | + } |
| 68 | + return &pb.Action, nil |
| 69 | +} |
| 70 | + |
| 71 | +func (c *PortClient) DeleteAction(ctx context.Context, blueprintID, actionID string) error { |
| 72 | + url := "v1/blueprints/{blueprint_identifier}/actions/{action_identifier}" |
| 73 | + resp, err := c.Client.R(). |
| 74 | + SetContext(ctx). |
| 75 | + SetHeader("Accept", "application/json"). |
| 76 | + SetPathParam("blueprint_identifier", blueprintID). |
| 77 | + SetPathParam("action_identifier", actionID). |
| 78 | + Delete(url) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + responseBody := make(map[string]interface{}) |
| 83 | + err = json.Unmarshal(resp.Body(), &responseBody) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + if !(responseBody["ok"].(bool)) { |
| 88 | + return fmt.Errorf("failed to delete action. got:\n%s", string(resp.Body())) |
| 89 | + } |
| 90 | + return nil |
| 91 | +} |
0 commit comments