/
oss_org_id.go
100 lines (87 loc) · 3.43 KB
/
oss_org_id.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
package grafana
import (
"fmt"
"strconv"
"strings"
gapi "github.com/grafana/grafana-api-golang-client"
goapi "github.com/grafana/grafana-openapi-client-go/client"
"github.com/grafana/terraform-provider-grafana/internal/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func orgIDAttribute() *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The Organization ID. If not set, the Org ID defined in the provider block will be used.",
ForceNew: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return new == "" // Ignore the case where we have a global org_id set
},
}
}
// MakeOrgResourceID creates a resource ID for an org-scoped resource
func MakeOrgResourceID(orgID int64, resourceID interface{}) string {
return fmt.Sprintf("%d:%s", orgID, fmt.Sprint(resourceID))
}
// SplitOrgResourceID splits into two parts (org ID and resource ID) the ID of an org-scoped resource
func SplitOrgResourceID(id string) (int64, string) {
if strings.ContainsRune(id, ':') {
parts := strings.SplitN(id, ":", 2)
orgID, _ := strconv.ParseInt(parts[0], 10, 64)
return orgID, parts[1]
}
return 0, id
}
// Deprecated: Use OAPIClientFromExistingOrgResource instead
func DeprecatedClientFromExistingOrgResource(meta interface{}, id string) (*gapi.Client, int64, string) {
orgID, restOfID := SplitOrgResourceID(id)
client := meta.(*common.Client).DeprecatedGrafanaAPI
if orgID == 0 {
orgID = meta.(*common.Client).GrafanaAPIConfig.OrgID // It's configured globally. TODO: Remove this once we drop support for the global org_id
} else if orgID > 0 {
client = client.WithOrgID(orgID)
}
return client, orgID, restOfID
}
// Deprecated: Use OAPIClientFromNewOrgResource instead
func DeprecatedClientFromNewOrgResource(meta interface{}, d *schema.ResourceData) (*gapi.Client, int64) {
orgID := parseOrgID(d)
client := meta.(*common.Client).DeprecatedGrafanaAPI
if orgID == 0 {
orgID = meta.(*common.Client).GrafanaAPIConfig.OrgID // It's configured globally. TODO: Remove this once we drop support for the global org_id
} else if orgID > 0 {
client = client.WithOrgID(orgID)
}
return client, orgID
}
// OAPIClientFromExistingOrgResource creates a client from the ID of an org-scoped resource
// Those IDs are in the <orgID>:<resourceID> format
func OAPIClientFromExistingOrgResource(meta interface{}, id string) (*goapi.GrafanaHTTPAPI, int64, string) {
orgID, restOfID := SplitOrgResourceID(id)
client := meta.(*common.Client).GrafanaOAPI.Clone()
if orgID == 0 {
orgID = client.OrgID()
} else if orgID > 0 {
client = client.WithOrgID(orgID)
}
return client, orgID, restOfID
}
// OAPIClientFromNewOrgResource creates an OpenAPI client from the `org_id` attribute of a resource
// This client is meant to be used in `Create` functions when the ID hasn't already been baked into the resource ID
func OAPIClientFromNewOrgResource(meta interface{}, d *schema.ResourceData) (*goapi.GrafanaHTTPAPI, int64) {
orgID := parseOrgID(d)
client := meta.(*common.Client).GrafanaOAPI.Clone()
if orgID == 0 {
orgID = client.OrgID()
} else if orgID > 0 {
client = client.WithOrgID(orgID)
}
return client, orgID
}
func OAPIGlobalClient(meta interface{}) *goapi.GrafanaHTTPAPI {
return meta.(*common.Client).GrafanaOAPI.Clone().WithOrgID(0)
}
func parseOrgID(d *schema.ResourceData) int64 {
orgID, _ := strconv.ParseInt(d.Get("org_id").(string), 10, 64)
return orgID
}