|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | +) |
| 11 | + |
| 12 | +var GroupMappingNotFound = errors.New("group mapping not found") |
| 13 | + |
| 14 | +func (client *sysdigCommonClient) CreateGroupMapping(ctx context.Context, request *GroupMapping) (*GroupMapping, error) { |
| 15 | + payload, err := request.ToJSON() |
| 16 | + if err != nil { |
| 17 | + return nil, err |
| 18 | + } |
| 19 | + |
| 20 | + response, err := client.doSysdigCommonRequest(ctx, http.MethodPost, client.CreateGroupMappingUrl(), payload) |
| 21 | + if err != nil { |
| 22 | + return nil, err |
| 23 | + } |
| 24 | + defer response.Body.Close() |
| 25 | + |
| 26 | + if response.StatusCode != http.StatusOK { |
| 27 | + return nil, errorFromResponse(response) |
| 28 | + } |
| 29 | + |
| 30 | + body, err := io.ReadAll(response.Body) |
| 31 | + if err != nil { |
| 32 | + return nil, err |
| 33 | + } |
| 34 | + |
| 35 | + var groupMapping GroupMapping |
| 36 | + err = json.Unmarshal(body, &groupMapping) |
| 37 | + if err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + |
| 41 | + return &groupMapping, nil |
| 42 | +} |
| 43 | + |
| 44 | +func (client *sysdigCommonClient) UpdateGroupMapping(ctx context.Context, request *GroupMapping, id int) (*GroupMapping, error) { |
| 45 | + payload, err := request.ToJSON() |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + |
| 50 | + response, err := client.doSysdigCommonRequest(ctx, http.MethodPut, client.UpdateGroupMappingUrl(id), payload) |
| 51 | + if err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + defer response.Body.Close() |
| 55 | + |
| 56 | + if response.StatusCode != http.StatusOK { |
| 57 | + return nil, errorFromResponse(response) |
| 58 | + } |
| 59 | + |
| 60 | + body, err := io.ReadAll(response.Body) |
| 61 | + if err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + |
| 65 | + var groupMapping GroupMapping |
| 66 | + err = json.Unmarshal(body, &groupMapping) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + |
| 71 | + return &groupMapping, nil |
| 72 | +} |
| 73 | + |
| 74 | +func (client *sysdigCommonClient) DeleteGroupMapping(ctx context.Context, id int) error { |
| 75 | + response, err := client.doSysdigCommonRequest(ctx, http.MethodDelete, client.DeleteGroupMappingUrl(id), nil) |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + defer response.Body.Close() |
| 80 | + |
| 81 | + if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotFound { |
| 82 | + return errorFromResponse(response) |
| 83 | + } |
| 84 | + |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +func (client *sysdigCommonClient) GetGroupMapping(ctx context.Context, id int) (*GroupMapping, error) { |
| 89 | + response, err := client.doSysdigCommonRequest(ctx, http.MethodGet, client.GetGroupMappingUrl(id), nil) |
| 90 | + if err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + defer response.Body.Close() |
| 94 | + |
| 95 | + if response.StatusCode != http.StatusOK { |
| 96 | + if response.StatusCode == http.StatusNotFound { |
| 97 | + return nil, GroupMappingNotFound |
| 98 | + } |
| 99 | + return nil, errorFromResponse(response) |
| 100 | + } |
| 101 | + |
| 102 | + body, err := io.ReadAll(response.Body) |
| 103 | + if err != nil { |
| 104 | + return nil, err |
| 105 | + } |
| 106 | + |
| 107 | + var groupMapping GroupMapping |
| 108 | + err = json.Unmarshal(body, &groupMapping) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + |
| 113 | + return &groupMapping, nil |
| 114 | +} |
| 115 | + |
| 116 | +func (client *sysdigCommonClient) GetGroupMappingUrl(id int) string { |
| 117 | + return fmt.Sprintf("%s/api/groupmappings/%d", client.URL, id) |
| 118 | +} |
| 119 | + |
| 120 | +func (client *sysdigCommonClient) CreateGroupMappingUrl() string { |
| 121 | + return fmt.Sprintf("%s/api/groupmappings", client.URL) |
| 122 | +} |
| 123 | + |
| 124 | +func (client *sysdigCommonClient) UpdateGroupMappingUrl(id int) string { |
| 125 | + return fmt.Sprintf("%s/api/groupmappings/%d", client.URL, id) |
| 126 | +} |
| 127 | + |
| 128 | +func (client *sysdigCommonClient) DeleteGroupMappingUrl(id int) string { |
| 129 | + return fmt.Sprintf("%s/api/groupmappings/%d", client.URL, id) |
| 130 | +} |
0 commit comments