|
| 1 | +/** |
| 2 | +* @license |
| 3 | +* Copyright 2020 Dynatrace LLC |
| 4 | +* |
| 5 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +* you may not use this file except in compliance with the License. |
| 7 | +* You may obtain a copy of the License at |
| 8 | +* |
| 9 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +* |
| 11 | +* Unless required by applicable law or agreed to in writing, software |
| 12 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +* See the License for the specific language governing permissions and |
| 15 | +* limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package segments |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "encoding/json" |
| 23 | + |
| 24 | + "github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api" |
| 25 | + "github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/rest" |
| 26 | + "github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/settings" |
| 27 | + "github.com/dynatrace-oss/terraform-provider-dynatrace/provider/logging" |
| 28 | + segmentsapi "github.com/dynatrace/dynatrace-configuration-as-code-core/api" |
| 29 | + "github.com/dynatrace/dynatrace-configuration-as-code-core/clients" |
| 30 | + segmentsclient "github.com/dynatrace/dynatrace-configuration-as-code-core/clients/segments" |
| 31 | + "golang.org/x/oauth2/clientcredentials" |
| 32 | + |
| 33 | + "github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api/automation/httplog" |
| 34 | + segments "github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api/grail/segments/settings" |
| 35 | +) |
| 36 | + |
| 37 | +func Service(credentials *settings.Credentials) settings.CRUDService[*segments.Segment] { |
| 38 | + return &service{credentials} |
| 39 | +} |
| 40 | + |
| 41 | +type service struct { |
| 42 | + credentials *settings.Credentials |
| 43 | +} |
| 44 | + |
| 45 | +func (me *service) client(ctx context.Context) *segmentsclient.Client { |
| 46 | + httplog.InstallRoundTripper() |
| 47 | + |
| 48 | + clientsFactory := clients.Factory(). |
| 49 | + WithPlatformURL(me.credentials.Automation.EnvironmentURL). |
| 50 | + WithOAuthCredentials(clientcredentials.Config{ |
| 51 | + ClientID: me.credentials.Automation.ClientID, |
| 52 | + ClientSecret: me.credentials.Automation.ClientSecret, |
| 53 | + TokenURL: me.credentials.Automation.TokenURL, |
| 54 | + }). |
| 55 | + WithUserAgent("Dynatrace Terraform Provider") |
| 56 | + |
| 57 | + segmentClient, _ := clientsFactory.SegmentsClient(ctx) |
| 58 | + return segmentClient |
| 59 | +} |
| 60 | + |
| 61 | +func (me *service) Get(ctx context.Context, id string, v *segments.Segment) (err error) { |
| 62 | + response, err := me.client(ctx).Get(ctx, id) |
| 63 | + if err != nil { |
| 64 | + if apiError, ok := err.(segmentsapi.APIError); ok { |
| 65 | + return rest.Error{Code: apiError.StatusCode, Message: apiError.Error()} |
| 66 | + } |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + return json.Unmarshal(response.Data, &v) |
| 71 | +} |
| 72 | + |
| 73 | +func (me *service) SchemaID() string { |
| 74 | + return "storage:filter-segments" |
| 75 | +} |
| 76 | + |
| 77 | +type SegmentStub struct { |
| 78 | + UID string `json:"uid"` |
| 79 | + Name string `json:"name"` |
| 80 | +} |
| 81 | + |
| 82 | +func (me *service) List(ctx context.Context) (api.Stubs, error) { |
| 83 | + listResponse, err := me.client(ctx).List(ctx) |
| 84 | + if err != nil { |
| 85 | + if apiError, ok := err.(segmentsapi.APIError); ok { |
| 86 | + return nil, rest.Error{Code: apiError.StatusCode, Message: apiError.Error()} |
| 87 | + } |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + var segments []SegmentStub |
| 91 | + if err := json.Unmarshal(listResponse.Data, &segments); err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + var stubs api.Stubs |
| 95 | + for _, segment := range segments { |
| 96 | + stubs = append(stubs, &api.Stub{ID: segment.UID, Name: segment.Name}) |
| 97 | + } |
| 98 | + |
| 99 | + return stubs, nil |
| 100 | +} |
| 101 | + |
| 102 | +func (me *service) Validate(_ *segments.Segment) error { |
| 103 | + return nil // no endpoint for that |
| 104 | +} |
| 105 | + |
| 106 | +func (me *service) Create(ctx context.Context, v *segments.Segment) (stub *api.Stub, err error) { |
| 107 | + var data []byte |
| 108 | + if data, err = json.Marshal(v); err != nil { |
| 109 | + return nil, err |
| 110 | + } |
| 111 | + logging.File.Println(string(data)) |
| 112 | + response, err := me.client(ctx).Create(ctx, data) |
| 113 | + if err != nil { |
| 114 | + if apiError, ok := err.(segmentsapi.APIError); ok { |
| 115 | + return nil, rest.Error{Code: apiError.StatusCode, Message: apiError.Error()} |
| 116 | + } |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + |
| 120 | + var segmentStub SegmentStub |
| 121 | + if err := json.Unmarshal(response.Data, &segmentStub); err != nil { |
| 122 | + return nil, err |
| 123 | + } |
| 124 | + |
| 125 | + return &api.Stub{Name: v.Name, ID: segmentStub.UID}, nil |
| 126 | +} |
| 127 | + |
| 128 | +func (me *service) Update(ctx context.Context, id string, v *segments.Segment) (err error) { |
| 129 | + var data []byte |
| 130 | + if data, err = json.Marshal(v); err != nil { |
| 131 | + return err |
| 132 | + } |
| 133 | + |
| 134 | + _, err = me.client(ctx).Update(ctx, id, data) |
| 135 | + if err != nil { |
| 136 | + if apiError, ok := err.(segmentsapi.APIError); ok { |
| 137 | + return rest.Error{Code: apiError.StatusCode, Message: apiError.Error()} |
| 138 | + } |
| 139 | + return err |
| 140 | + } |
| 141 | + |
| 142 | + return nil |
| 143 | +} |
| 144 | + |
| 145 | +func (me *service) Delete(ctx context.Context, id string) error { |
| 146 | + _, err := me.client(ctx).Delete(ctx, id) |
| 147 | + if err != nil { |
| 148 | + if apiError, ok := err.(segmentsapi.APIError); ok { |
| 149 | + return rest.Error{Code: apiError.StatusCode, Message: apiError.Error()} |
| 150 | + } |
| 151 | + return err |
| 152 | + } |
| 153 | + |
| 154 | + return nil |
| 155 | +} |
| 156 | + |
| 157 | +func (me *service) New() *segments.Segment { |
| 158 | + return new(segments.Segment) |
| 159 | +} |
0 commit comments