|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | +) |
| 8 | + |
| 9 | +func (c *PortClient) ReadBlueprint(ctx context.Context, id string) (*Blueprint, error) { |
| 10 | + pb := &PortBody{} |
| 11 | + url := "v0.1/blueprints/{identifier}" |
| 12 | + resp, err := c.Client.R(). |
| 13 | + SetContext(ctx). |
| 14 | + SetHeader("Accept", "application/json"). |
| 15 | + SetQueryParam("exclude_mirror_properties", "true"). |
| 16 | + SetResult(pb). |
| 17 | + SetPathParam("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 blueprint, got: %s", resp.Body()) |
| 24 | + } |
| 25 | + return &pb.Blueprint, nil |
| 26 | +} |
| 27 | + |
| 28 | +func (c *PortClient) CreateBlueprint(ctx context.Context, b *Blueprint) (*Blueprint, error) { |
| 29 | + url := "v0.1/blueprints" |
| 30 | + resp, err := c.Client.R(). |
| 31 | + SetBody(b). |
| 32 | + SetContext(ctx). |
| 33 | + Post(url) |
| 34 | + if err != nil { |
| 35 | + return nil, err |
| 36 | + } |
| 37 | + var pb PortBody |
| 38 | + err = json.Unmarshal(resp.Body(), &pb) |
| 39 | + if err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + if !pb.OK { |
| 43 | + return nil, fmt.Errorf("failed to create blueprint, got: %s", resp.Body()) |
| 44 | + } |
| 45 | + return &pb.Blueprint, nil |
| 46 | +} |
| 47 | + |
| 48 | +func (c *PortClient) UpdateBlueprint(ctx context.Context, b *Blueprint, id string) (*Blueprint, error) { |
| 49 | + url := "v0.1/blueprints/{identifier}" |
| 50 | + resp, err := c.Client.R(). |
| 51 | + SetBody(b). |
| 52 | + SetContext(ctx). |
| 53 | + SetPathParam("identifier", id). |
| 54 | + Put(url) |
| 55 | + if err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + var pb PortBody |
| 59 | + err = json.Unmarshal(resp.Body(), &pb) |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + if !pb.OK { |
| 64 | + return nil, fmt.Errorf("failed to create blueprint, got: %s", resp.Body()) |
| 65 | + } |
| 66 | + return &pb.Blueprint, nil |
| 67 | +} |
| 68 | + |
| 69 | +func (c *PortClient) DeleteBlueprint(ctx context.Context, id string) error { |
| 70 | + url := "v0.1/blueprints/{identifier}" |
| 71 | + resp, err := c.Client.R(). |
| 72 | + SetContext(ctx). |
| 73 | + SetHeader("Accept", "application/json"). |
| 74 | + SetPathParam("identifier", id). |
| 75 | + Delete(url) |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + responseBody := make(map[string]interface{}) |
| 80 | + err = json.Unmarshal(resp.Body(), &responseBody) |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + if !(responseBody["ok"].(bool)) { |
| 85 | + return fmt.Errorf("failed to delete blueprint. got:\n%s", string(resp.Body())) |
| 86 | + } |
| 87 | + return nil |
| 88 | +} |
0 commit comments