This repository has been archived by the owner on May 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
orgs.go
144 lines (123 loc) · 4 KB
/
orgs.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package orgs
import (
"encoding/json"
"fmt"
"net/url"
"path"
"strings"
"github.com/srinandan/apigeecli/apiclient"
)
var analyticsRegions = [...]string{"asia-east1", "asia-east1", "asia-northeast1", "asia-southeast1",
"europe-west1", "us-central1", "us-east1", "us-east4", "us-west1", "australia-southeast1",
"europe-west2"}
func validRegion(region string) bool {
for _, r := range analyticsRegions {
if region == r {
return true
}
}
return false
}
//Create
func Create(region string) (respBody []byte, err error) {
const baseURL = "https://apigee.googleapis.com/v1/organizations"
if !validRegion(region) {
return respBody, fmt.Errorf("invalid analytics region."+
" Analytics region must be one of : %v", analyticsRegions)
}
u, _ := url.Parse(baseURL)
u.Path = path.Join(u.Path)
q := u.Query()
q.Set("parent", "projects/"+apiclient.GetProjectID())
u.RawQuery = q.Encode()
orgPayload := []string{}
orgPayload = append(orgPayload, "\"name\":\""+apiclient.GetApigeeOrg()+"\"")
orgPayload = append(orgPayload, "\"analyticsRegion\":\""+region+"\"")
payload := "{" + strings.Join(orgPayload, ",") + "}"
respBody, err = apiclient.HttpClient(apiclient.GetPrintOutput(), u.String(), payload)
return respBody, err
}
//Get
func Get() (respBody []byte, err error) {
u, _ := url.Parse(apiclient.BaseURL)
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg())
respBody, err = apiclient.HttpClient(apiclient.GetPrintOutput(), u.String())
return respBody, err
}
//List
func List() (respBody []byte, err error) {
u, _ := url.Parse(apiclient.BaseURL)
respBody, err = apiclient.HttpClient(apiclient.GetPrintOutput(), u.String())
return respBody, err
}
//SetOrgProperty is used to set org properties
func SetOrgProperty(name string, value string) (err error) {
//OrgProperty contains an individual org flag or property
type orgProperty struct {
Name string `json:"name,omitempty"`
Value string `json:"value,omitempty"`
}
//OrgProperties stores all the org feature flags and properties
type orgProperties struct {
Property []orgProperty `json:"property,omitempty"`
}
//Org structure
type organization struct {
Name string `json:"name,omitempty"`
CreatedAt string `json:"-,omitempty"`
LastModifiedAt string `json:"-,omitempty"`
Environments []string `json:"-,omitempty"`
Properties orgProperties `json:"properties,omitempty"`
AnalyticsRegion string `json:"-,omitempty"`
}
u, _ := url.Parse(apiclient.BaseURL)
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg())
//get org details
orgBody, err := apiclient.HttpClient(false, u.String())
if err != nil {
return err
}
org := organization{}
err = json.Unmarshal(orgBody, &org)
if err != nil {
return err
}
//check if the property exists
found := false
for i, properties := range org.Properties.Property {
if properties.Name == name {
fmt.Println("Property found, enabling property")
org.Properties.Property[i].Value = value
found = true
break
}
}
if !found {
//set the property
newProp := orgProperty{}
newProp.Name = name
newProp.Value = value
org.Properties.Property = append(org.Properties.Property, newProp)
}
newOrgBody, err := json.Marshal(org)
if err != nil {
return err
}
u, _ = url.Parse(apiclient.BaseURL)
u.Path = path.Join(u.Path, apiclient.GetApigeeOrg())
_, err = apiclient.HttpClient(apiclient.GetPrintOutput(), u.String(), string(newOrgBody), "PUT")
return err
}