-
Notifications
You must be signed in to change notification settings - Fork 48
/
organization.go
112 lines (94 loc) · 3.49 KB
/
organization.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
112
package v2
import (
"context"
"fmt"
"net/http"
)
const (
organizationsPath = "%s/api/cloudauth/v1/organizations"
organizationPath = "%s/api/cloudauth/v1/organizations/%s"
)
type OrganizationSecureInterface interface {
Base
CreateOrganizationSecure(ctx context.Context, org *OrganizationSecure) (*OrganizationSecure, string, error)
GetOrganizationSecure(ctx context.Context, orgID string) (*OrganizationSecure, string, error)
DeleteOrganizationSecure(ctx context.Context, orgID string) (string, error)
UpdateOrganizationSecure(ctx context.Context, orgID string, org *OrganizationSecure) (*OrganizationSecure, string, error)
}
func (client *Client) CreateOrganizationSecure(ctx context.Context, org *OrganizationSecure) (*OrganizationSecure, string, error) {
payload, err := client.marshalCloudauthProto(org)
if err != nil {
return nil, "", err
}
response, err := client.requester.Request(ctx, http.MethodPost, client.organizationsURL(), payload)
if err != nil {
return nil, "", err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated && response.StatusCode != http.StatusAccepted {
errStatus, err := client.ErrorAndStatusFromResponse(response)
return nil, errStatus, err
}
organization := &OrganizationSecure{}
err = client.unmarshalCloudauthProto(response.Body, organization)
if err != nil {
return nil, "", err
}
return organization, "", nil
}
func (client *Client) GetOrganizationSecure(ctx context.Context, orgID string) (*OrganizationSecure, string, error) {
response, err := client.requester.Request(ctx, http.MethodGet, client.organizationURL(orgID), nil)
if err != nil {
return nil, "", err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
errStatus, err := client.ErrorAndStatusFromResponse(response)
return nil, errStatus, err
}
organization := &OrganizationSecure{}
err = client.unmarshalCloudauthProto(response.Body, organization)
if err != nil {
return nil, "", err
}
return organization, "", nil
}
func (client *Client) DeleteOrganizationSecure(ctx context.Context, orgID string) (string, error) {
response, err := client.requester.Request(ctx, http.MethodDelete, client.organizationURL(orgID), nil)
if err != nil {
return "", err
}
defer response.Body.Close()
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK {
errStatus, err := client.ErrorAndStatusFromResponse(response)
return errStatus, err
}
return "", nil
}
func (client *Client) UpdateOrganizationSecure(ctx context.Context, orgID string, org *OrganizationSecure) (*OrganizationSecure, string, error) {
payload, err := Marshal(org)
if err != nil {
return nil, "", err
}
response, err := client.requester.Request(ctx, http.MethodPut, client.organizationURL(orgID), payload)
if err != nil {
return nil, "", err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated && response.StatusCode != http.StatusAccepted {
errStatus, err := client.ErrorAndStatusFromResponse(response)
return nil, errStatus, err
}
organization := &OrganizationSecure{}
err = client.unmarshalCloudauthProto(response.Body, organization)
if err != nil {
return nil, "", err
}
return organization, "", nil
}
func (client *Client) organizationsURL() string {
return fmt.Sprintf(organizationsPath, client.config.url)
}
func (client *Client) organizationURL(orgId string) string {
return fmt.Sprintf(organizationPath, client.config.url, orgId)
}