|
| 1 | +package boundaries |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api" |
| 10 | + "github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api/iam" |
| 11 | + boundaries "github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api/iam/boundaries/settings" |
| 12 | + "github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/settings" |
| 13 | +) |
| 14 | + |
| 15 | +func Service(credentials *settings.Credentials) settings.CRUDService[*boundaries.PolicyBoundary] { |
| 16 | + return &BoundaryServiceClient{ |
| 17 | + clientID: credentials.IAM.ClientID, |
| 18 | + accountID: credentials.IAM.AccountID, |
| 19 | + clientSecret: credentials.IAM.ClientSecret, |
| 20 | + tokenURL: credentials.IAM.TokenURL, |
| 21 | + endpointURL: credentials.IAM.EndpointURL, |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +type BoundaryServiceClient struct { |
| 26 | + clientID string |
| 27 | + accountID string |
| 28 | + clientSecret string |
| 29 | + tokenURL string |
| 30 | + endpointURL string |
| 31 | +} |
| 32 | + |
| 33 | +func (me *BoundaryServiceClient) ClientID() string { |
| 34 | + return me.clientID |
| 35 | +} |
| 36 | + |
| 37 | +func (me *BoundaryServiceClient) AccountID() string { |
| 38 | + return me.accountID |
| 39 | +} |
| 40 | + |
| 41 | +func (me *BoundaryServiceClient) ClientSecret() string { |
| 42 | + return me.clientSecret |
| 43 | +} |
| 44 | + |
| 45 | +func (me *BoundaryServiceClient) TokenURL() string { |
| 46 | + return me.tokenURL |
| 47 | +} |
| 48 | + |
| 49 | +func (me *BoundaryServiceClient) EndpointURL() string { |
| 50 | + return me.endpointURL |
| 51 | +} |
| 52 | + |
| 53 | +func (me *BoundaryServiceClient) List(ctx context.Context) (api.Stubs, error) { |
| 54 | + var err error |
| 55 | + var responseBytes []byte |
| 56 | + |
| 57 | + client := iam.NewIAMClient(me) |
| 58 | + |
| 59 | + if responseBytes, err = client.GET(ctx, fmt.Sprintf("%s/iam/v1/repo/account/%s/boundaries", me.endpointURL, strings.TrimPrefix(me.AccountID(), "urn:dtaccount:")), 200, false); err != nil { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + |
| 63 | + var response ListPolicyBoundariesResponse |
| 64 | + if err = json.Unmarshal(responseBytes, &response); err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + if len(response.PolicyBoundaries) == 0 { |
| 68 | + return api.Stubs{}, nil |
| 69 | + } |
| 70 | + stubs := api.Stubs{} |
| 71 | + for _, boundary := range response.PolicyBoundaries { |
| 72 | + stubs = append(stubs, &api.Stub{ID: boundary.UUID, Name: boundary.Name}) |
| 73 | + } |
| 74 | + return stubs, nil |
| 75 | +} |
| 76 | + |
| 77 | +func (me *BoundaryServiceClient) Get(ctx context.Context, id string, v *boundaries.PolicyBoundary) error { |
| 78 | + var err error |
| 79 | + var responseBytes []byte |
| 80 | + |
| 81 | + client := iam.NewIAMClient(me) |
| 82 | + |
| 83 | + if responseBytes, err = client.GET(ctx, fmt.Sprintf("%s/iam/v1/repo/account/%s/boundaries/%s", me.endpointURL, strings.TrimPrefix(me.AccountID(), "urn:dtaccount:"), id), 200, false); err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + |
| 87 | + if err = json.Unmarshal(responseBytes, v); err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + return nil |
| 91 | +} |
| 92 | + |
| 93 | +func (me *BoundaryServiceClient) SchemaID() string { |
| 94 | + return "accounts:iam:boundaries" |
| 95 | +} |
| 96 | + |
| 97 | +func (me *BoundaryServiceClient) Create(ctx context.Context, v *boundaries.PolicyBoundary) (*api.Stub, error) { |
| 98 | + var err error |
| 99 | + var responseBytes []byte |
| 100 | + |
| 101 | + client := iam.NewIAMClient(me) |
| 102 | + |
| 103 | + if responseBytes, err = client.POST( |
| 104 | + ctx, |
| 105 | + fmt.Sprintf("%s/iam/v1/repo/account/%s/boundaries", me.endpointURL, strings.TrimPrefix(me.AccountID(), "urn:dtaccount:")), |
| 106 | + v, |
| 107 | + 201, |
| 108 | + false, |
| 109 | + ); err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + |
| 113 | + response := struct { |
| 114 | + UUID string `json:"uuid"` |
| 115 | + Name string `json:"name"` |
| 116 | + }{} |
| 117 | + |
| 118 | + if err = json.Unmarshal(responseBytes, &response); err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + |
| 122 | + return &api.Stub{ID: response.UUID, Name: response.Name}, nil |
| 123 | +} |
| 124 | + |
| 125 | +func (me *BoundaryServiceClient) Update(ctx context.Context, id string, v *boundaries.PolicyBoundary) error { |
| 126 | + var err error |
| 127 | + var responseBytes []byte |
| 128 | + |
| 129 | + client := iam.NewIAMClient(me) |
| 130 | + |
| 131 | + if responseBytes, err = client.PUT( |
| 132 | + ctx, |
| 133 | + fmt.Sprintf("%s/iam/v1/repo/account/%s/boundaries/%s", me.endpointURL, strings.TrimPrefix(me.AccountID(), "urn:dtaccount:"), id), |
| 134 | + v, |
| 135 | + 201, |
| 136 | + false, |
| 137 | + ); err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + |
| 141 | + response := struct { |
| 142 | + UUID string `json:"uuid"` |
| 143 | + Name string `json:"name"` |
| 144 | + }{} |
| 145 | + |
| 146 | + if err = json.Unmarshal(responseBytes, &response); err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + |
| 150 | + return nil |
| 151 | +} |
| 152 | + |
| 153 | +func (me *BoundaryServiceClient) Delete(ctx context.Context, id string) error { |
| 154 | + var err error |
| 155 | + |
| 156 | + client := iam.NewIAMClient(me) |
| 157 | + |
| 158 | + if _, err = client.DELETE( |
| 159 | + ctx, |
| 160 | + fmt.Sprintf("%s/iam/v1/repo/account/%s/boundaries/%s", me.endpointURL, strings.TrimPrefix(me.AccountID(), "urn:dtaccount:"), id), |
| 161 | + 204, |
| 162 | + false, |
| 163 | + ); err != nil { |
| 164 | + return err |
| 165 | + } |
| 166 | + |
| 167 | + return nil |
| 168 | +} |
| 169 | + |
| 170 | +type PolicyBoundary struct { |
| 171 | + UUID string `json:"uuid"` |
| 172 | + LevelType string `json:"levelType"` |
| 173 | + LevelID string `json:"levelId"` |
| 174 | + Name string `json:"name"` |
| 175 | + Query string `json:"boundaryQuery"` |
| 176 | +} |
| 177 | + |
| 178 | +type ListPolicyBoundariesResponse struct { |
| 179 | + PageSize int `json:"pageSize"` |
| 180 | + PageNumber int `json:"pageNumber"` |
| 181 | + TotalCount int `json:"totalCount"` |
| 182 | + PolicyBoundaries []PolicyBoundary `json:"content"` |
| 183 | +} |
0 commit comments