-
Notifications
You must be signed in to change notification settings - Fork 31
/
service.go
111 lines (89 loc) · 2.96 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package groups
import (
"fmt"
"github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api"
groups "github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api/cluster/v1/groups/settings"
"github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/rest"
"github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/settings"
)
const SchemaID = "accounts:groups"
func Service(credentials *settings.Credentials) settings.CRUDService[*groups.GroupConfig] {
return &service{
serviceClient: NewService(fmt.Sprintf("%s%s", credentials.Cluster.URL, "/api/v1.0/onpremise"), credentials.Cluster.Token),
}
}
// ServiceClient TODO: documentation
type ServiceClient struct {
client rest.Client
}
// NewService creates a new Service Client
// baseURL should look like this: "https://#######.live.dynatrace.com/api/config/v1"
// token is an API Token
func NewService(baseURL string, token string) *ServiceClient {
return &ServiceClient{client: rest.DefaultClient(baseURL, token)}
}
type service struct {
serviceClient *ServiceClient
}
func (me *service) Create(v *groups.GroupConfig) (*api.Stub, error) {
return me.serviceClient.Create(v)
}
func (me *service) Update(id string, v *groups.GroupConfig) error {
return me.serviceClient.Update(v)
}
func (me *service) Delete(id string) error {
return me.serviceClient.Delete(id)
}
func (me *service) List() (api.Stubs, error) {
return me.serviceClient.List()
}
func (me *service) Get(id string, v *groups.GroupConfig) error {
return me.serviceClient.Get(id, v)
}
func (me *service) Name() string {
return me.SchemaID()
}
func (me *service) SchemaID() string {
return SchemaID
}
func (cs *ServiceClient) SchemaID() string {
return SchemaID
}
// Create TODO: documentation
func (cs *ServiceClient) Create(groupConfig *groups.GroupConfig) (*api.Stub, error) {
var err error
var createdGroupConfig groups.GroupConfig
if err = cs.client.Post("/groups", groupConfig, 200).Finish(&createdGroupConfig); err != nil {
return nil, err
}
return &api.Stub{ID: *createdGroupConfig.ID, Name: createdGroupConfig.Name}, nil
}
// Update TODO: documentation
func (cs *ServiceClient) Update(groupConfig *groups.GroupConfig) error {
return cs.client.Put("/groups", groupConfig, 200).Finish()
}
// Delete TODO: documentation
func (cs *ServiceClient) Delete(id string) error {
return cs.client.Delete(fmt.Sprintf("/groups/%s", id), 200).Finish()
}
// Get TODO: documentation
func (cs *ServiceClient) Get(id string, v *groups.GroupConfig) error {
var err error
if err = cs.client.Get(fmt.Sprintf("/groups/%s", id), 200).Finish(&v); err != nil {
return err
}
return nil
}
// ListAll TODO: documentation
func (cs *ServiceClient) List() (api.Stubs, error) {
var err error
var stubs api.Stubs
var groups []*groups.GroupConfig
if err = cs.client.Get("/groups", 200).Finish(&groups); err != nil {
return nil, err
}
for _, group := range groups {
stubs = append(stubs, &api.Stub{ID: *group.ID, Name: group.Name})
}
return stubs, nil
}