Skip to content

Commit

Permalink
Cloud: Completely get rid of old client
Browse files Browse the repository at this point in the history
  • Loading branch information
julienduchesne committed Feb 5, 2024
1 parent 97af967 commit ade9d4c
Show file tree
Hide file tree
Showing 22 changed files with 229 additions and 258 deletions.
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,6 @@ provider_installation {
```
2. Run `go build` in this directory to get the binary, Terraform will use the binary you just built (it should print out a warning)

## Testing the `grafana-api-golang-client` Together with the Terraform Provider
As for testing the client, make a branch and open the provider PR with a `TODO:` to remove the replace operator, because the best way to test the changes you just made in the client is to integrate everything in the provider

1. create a branch `api-client-branchname` with your changes
2. modify the provider `go.mod`:
- replace github.com/grafana/grafana-api-golang-client => github.com/grafana/grafana-api-golang-client <api-client-branchname>
3. run `go mod tidy` in this directory
4. pushing allows you to run the provider tests in CI

### Running Tests

Acceptance tests require a running instance of Grafana. You can either handle
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ require (
github.com/go-openapi/runtime v0.27.1
github.com/go-openapi/strfmt v0.22.0
github.com/grafana/amixr-api-go-client v0.0.11
github.com/grafana/grafana-api-golang-client v0.27.0
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20240131153028-3f1ec24f35fa
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20240205182306-412960edbffd
github.com/grafana/grafana-openapi-client-go v0.0.0-20240126032018-bd23c00af697
github.com/grafana/machine-learning-go-client v0.5.0
github.com/grafana/slo-openapi-client/go v0.0.0-20240112175006-de02e75b9d73
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ github.com/go-openapi/validate v0.22.6 h1:+NhuwcEYpWdO5Nm4bmvhGLW0rt1Fcc532Mu3wp
github.com/go-openapi/validate v0.22.6/go.mod h1:eaddXSqKeTg5XpSmj1dYyFTK/95n/XHwcOY+BMxKMyM=
github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/gobs/pretty v0.0.0-20180724170744-09732c25a95b h1:/vQ+oYKu+JoyaMPDsv5FzwuL2wwWBgBbtj/YLCi4LuA=
github.com/gobs/pretty v0.0.0-20180724170744-09732c25a95b/go.mod h1:Xo4aNUOrJnVruqWQJBtW6+bTBDTniY8yZum5rF3b5jw=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
Expand All @@ -100,10 +98,8 @@ github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grafana/amixr-api-go-client v0.0.11 h1:jlE+5t0tRuCtjbpM81j70Dr2J4eCySuWyNGdfLMGdhE=
github.com/grafana/amixr-api-go-client v0.0.11/go.mod h1:N6x26XUrM5zGtK5zL5vNJnAn2JFMxLFPPLTw/6pDkFE=
github.com/grafana/grafana-api-golang-client v0.27.0 h1:zIwMXcbCB4n588i3O2N6HfNcQogCNTd/vPkEXTr7zX8=
github.com/grafana/grafana-api-golang-client v0.27.0/go.mod h1:uNLZEmgKtTjHBtCQMwNn3qsx2mpMb8zU+7T4Xv3NR9Y=
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20240131153028-3f1ec24f35fa h1:mip69FpVOw1HM3GMA5DlwjSFmV22n1qrksgG/tt02BU=
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20240131153028-3f1ec24f35fa/go.mod h1:6sYY1qgwYfSDNQhKQA0tar8Oc38cIGfyqwejhxoOsPs=
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20240205182306-412960edbffd h1:/WTezOL1Zimx+/tGkqKwaT59BrXcTGLCCalg9FEmHXY=
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20240205182306-412960edbffd/go.mod h1:6sYY1qgwYfSDNQhKQA0tar8Oc38cIGfyqwejhxoOsPs=
github.com/grafana/grafana-openapi-client-go v0.0.0-20240126032018-bd23c00af697 h1:8io6OuKJrhH8SEfwXV1ZpAwgaGhJ25/pjaA3vYmMxxE=
github.com/grafana/grafana-openapi-client-go v0.0.0-20240126032018-bd23c00af697/go.mod h1:af7rlJw/VtbvAfI5VWzYO4p/pT58FXrN6XqZBnkwBxo=
github.com/grafana/machine-learning-go-client v0.5.0 h1:Q1K+MPSy8vfMm2jsk3WQ7O77cGr2fM5hxwtPSoPc5NU=
Expand Down
10 changes: 4 additions & 6 deletions internal/common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"sync"

onCallAPI "github.com/grafana/amixr-api-go-client"
gapi "github.com/grafana/grafana-api-golang-client"
"github.com/grafana/grafana-com-public-clients/go/gcom"
goapi "github.com/grafana/grafana-openapi-client-go/client"
"github.com/grafana/machine-learning-go-client/mlapi"
Expand All @@ -18,11 +17,10 @@ import (
)

type Client struct {
GrafanaAPIURL string
GrafanaAPIURLParsed *url.URL
GrafanaAPIConfig *goapi.TransportConfig
GrafanaCloudAPI *gapi.Client
GrafanaCloudAPIOpenAPI *gcom.APIClient
GrafanaAPIURL string
GrafanaAPIURLParsed *url.URL
GrafanaAPIConfig *goapi.TransportConfig
GrafanaCloudAPI *gcom.APIClient

GrafanaOAPI *goapi.GrafanaHTTPAPI

Expand Down
25 changes: 6 additions & 19 deletions internal/provider/configure_clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"time"

onCallAPI "github.com/grafana/amixr-api-go-client"
gapi "github.com/grafana/grafana-api-golang-client"
"github.com/grafana/grafana-com-public-clients/go/gcom"
goapi "github.com/grafana/grafana-openapi-client-go/client"
"github.com/grafana/machine-learning-go-client/mlapi"
Expand Down Expand Up @@ -139,22 +138,6 @@ func createSLOClient(client *common.Client, providerConfig frameworkProviderConf
}

func createCloudClient(client *common.Client, providerConfig frameworkProviderConfig) error {
// Configure old client
// TODO: Remove this once the old client is no longer used
cfg := gapi.Config{
APIKey: providerConfig.CloudAPIKey.ValueString(),
NumRetries: int(providerConfig.Retries.ValueInt64()),
RetryTimeout: time.Second * time.Duration(providerConfig.RetryWait.ValueInt64()),
}
var err error
if cfg.HTTPHeaders, err = getHTTPHeadersMap(providerConfig); err != nil {
return err
}
if client.GrafanaCloudAPI, err = gapi.New(providerConfig.CloudAPIURL.ValueString(), cfg); err != nil {
return err
}

// Configure new client (OpenAPI)
openAPIConfig := gcom.NewConfiguration()
parsedURL, err := url.Parse(providerConfig.CloudAPIURL.ValueString())
if err != nil {
Expand All @@ -164,10 +147,14 @@ func createCloudClient(client *common.Client, providerConfig frameworkProviderCo
openAPIConfig.Scheme = "https"
openAPIConfig.HTTPClient = getRetryClient(providerConfig)
openAPIConfig.DefaultHeader["Authorization"] = "Bearer " + providerConfig.CloudAPIKey.ValueString()
for k, v := range cfg.HTTPHeaders {
httpHeaders, err := getHTTPHeadersMap(providerConfig)
if err != nil {
return err
}
for k, v := range httpHeaders {
openAPIConfig.DefaultHeader[k] = v
}
client.GrafanaCloudAPIOpenAPI = gcom.NewAPIClient(openAPIConfig)
client.GrafanaCloudAPI = gcom.NewAPIClient(openAPIConfig)

return nil
}
Expand Down
11 changes: 5 additions & 6 deletions internal/resources/cloud/data_source_cloud_organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cloud
import (
"context"
"strconv"
"time"

"github.com/grafana/terraform-provider-grafana/internal/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -51,19 +50,19 @@ func DataSourceOrganizationRead(ctx context.Context, d *schema.ResourceData, met
if id == "" {
id = d.Get("slug").(string)
}
org, err := client.GetCloudOrg(id)
org, _, err := client.OrgsAPI.GetOrg(ctx, id).Execute()
if err != nil {
return apiError(err)
}

id = strconv.FormatInt(org.ID, 10)
id = strconv.FormatInt(int64(org.Id), 10)
d.SetId(id)
d.Set("id", id)
d.Set("name", org.Name)
d.Set("slug", org.Slug)
d.Set("url", org.URL)
d.Set("created_at", org.CreatedAt.Format(time.RFC3339))
d.Set("updated_at", org.UpdatedAt.Format(time.RFC3339))
d.Set("url", org.Url)
d.Set("created_at", org.CreatedAt)
d.Set("updated_at", org.UpdatedAt)

return nil
}
2 changes: 1 addition & 1 deletion internal/resources/cloud/data_source_cloud_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ available at “https://<stack_slug>.grafana.net".`,
}

func DataSourceStackRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*common.Client).GrafanaCloudAPIOpenAPI
client := meta.(*common.Client).GrafanaCloudAPI

slug := d.Get("slug").(string)

Expand Down
49 changes: 27 additions & 22 deletions internal/resources/cloud/resource_cloud_access_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
"time"

gapi "github.com/grafana/grafana-api-golang-client"
"github.com/grafana/grafana-com-public-clients/go/gcom"
"github.com/grafana/terraform-provider-grafana/internal/common"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -128,17 +128,19 @@ func CreateCloudAccessPolicy(ctx context.Context, d *schema.ResourceData, meta i
displayName = d.Get("name").(string)
}

result, err := client.CreateCloudAccessPolicy(region, gapi.CreateCloudAccessPolicyInput{
Name: d.Get("name").(string),
DisplayName: displayName,
Scopes: common.ListToStringSlice(d.Get("scopes").(*schema.Set).List()),
Realms: expandCloudAccessPolicyRealm(d.Get("realm").(*schema.Set).List()),
})
req := client.AccesspoliciesAPI.PostAccessPolicies(ctx).Region(region).XRequestId(ClientRequestID()).
PostAccessPoliciesRequest(gcom.PostAccessPoliciesRequest{
Name: d.Get("name").(string),
DisplayName: &displayName,
Scopes: common.ListToStringSlice(d.Get("scopes").(*schema.Set).List()),
Realms: expandCloudAccessPolicyRealm(d.Get("realm").(*schema.Set).List()),
})
result, _, err := req.Execute()
if err != nil {
return apiError(err)
}

d.SetId(fmt.Sprintf("%s/%s", region, result.ID))
d.SetId(fmt.Sprintf("%s/%s", region, *result.Id))

return ReadCloudAccessPolicy(ctx, d, meta)
}
Expand All @@ -152,11 +154,13 @@ func UpdateCloudAccessPolicy(ctx context.Context, d *schema.ResourceData, meta i
displayName = d.Get("name").(string)
}

_, err := client.UpdateCloudAccessPolicy(region, id, gapi.UpdateCloudAccessPolicyInput{
DisplayName: displayName,
Scopes: common.ListToStringSlice(d.Get("scopes").(*schema.Set).List()),
Realms: expandCloudAccessPolicyRealm(d.Get("realm").(*schema.Set).List()),
})
req := client.AccesspoliciesAPI.PostAccessPolicy(ctx, id).Region(region).XRequestId(ClientRequestID()).
PostAccessPolicyRequest(gcom.PostAccessPolicyRequest{
DisplayName: &displayName,
Scopes: common.ListToStringSlice(d.Get("scopes").(*schema.Set).List()),
Realms: expandCloudAccessPolicyRealm(d.Get("realm").(*schema.Set).List()),
})
_, _, err := req.Execute()
if err != nil {
return apiError(err)
}
Expand All @@ -168,13 +172,13 @@ func ReadCloudAccessPolicy(ctx context.Context, d *schema.ResourceData, meta int
client := meta.(*common.Client).GrafanaCloudAPI

region, id, _ := strings.Cut(d.Id(), "/")
result, err := client.CloudAccessPolicyByID(region, id)
result, _, err := client.AccesspoliciesAPI.GetAccessPolicy(ctx, id).Region(region).Execute()
if err, shouldReturn := common.CheckReadError("access policy", d, err); shouldReturn {
return err
}

d.Set("region", region)
d.Set("policy_id", result.ID)
d.Set("policy_id", result.Id)
d.Set("name", result.Name)
d.Set("display_name", result.DisplayName)
d.Set("scopes", result.Scopes)
Expand All @@ -188,7 +192,8 @@ func ReadCloudAccessPolicy(ctx context.Context, d *schema.ResourceData, meta int
func DeleteCloudAccessPolicy(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*common.Client).GrafanaCloudAPI
region, id, _ := strings.Cut(d.Id(), "/")
return apiError(client.DeleteCloudAccessPolicy(region, id))
_, _, err := client.AccesspoliciesAPI.DeleteAccessPolicy(ctx, id).Region(region).XRequestId(ClientRequestID()).Execute()
return apiError(err)
}

func validateCloudAccessPolicyScope(v interface{}, path cty.Path) diag.Diagnostics {
Expand All @@ -199,7 +204,7 @@ func validateCloudAccessPolicyScope(v interface{}, path cty.Path) diag.Diagnosti
return nil
}

func flattenCloudAccessPolicyRealm(realm []gapi.CloudAccessPolicyRealm) []interface{} {
func flattenCloudAccessPolicyRealm(realm []gcom.AuthAccessPolicyRealmsInner) []interface{} {
var result []interface{}

for _, r := range realm {
Expand All @@ -219,20 +224,20 @@ func flattenCloudAccessPolicyRealm(realm []gapi.CloudAccessPolicyRealm) []interf
return result
}

func expandCloudAccessPolicyRealm(realm []interface{}) []gapi.CloudAccessPolicyRealm {
var result []gapi.CloudAccessPolicyRealm
func expandCloudAccessPolicyRealm(realm []interface{}) []gcom.PostAccessPoliciesRequestRealmsInner {
var result []gcom.PostAccessPoliciesRequestRealmsInner

for _, r := range realm {
r := r.(map[string]interface{})
labelPolicy := []gapi.CloudAccessPolicyLabelPolicy{}
labelPolicy := []gcom.PostAccessPoliciesRequestRealmsInnerLabelPoliciesInner{}
for _, lp := range r["label_policy"].(*schema.Set).List() {
lp := lp.(map[string]interface{})
labelPolicy = append(labelPolicy, gapi.CloudAccessPolicyLabelPolicy{
labelPolicy = append(labelPolicy, gcom.PostAccessPoliciesRequestRealmsInnerLabelPoliciesInner{
Selector: lp["selector"].(string),
})
}

result = append(result, gapi.CloudAccessPolicyRealm{
result = append(result, gcom.PostAccessPoliciesRequestRealmsInner{
Type: r["type"].(string),
Identifier: r["identifier"].(string),
LabelPolicies: labelPolicy,
Expand Down
26 changes: 14 additions & 12 deletions internal/resources/cloud/resource_cloud_access_policy_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
"time"

gapi "github.com/grafana/grafana-api-golang-client"
"github.com/grafana/grafana-com-public-clients/go/gcom"
"github.com/grafana/terraform-provider-grafana/internal/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -92,10 +92,10 @@ func CreateCloudAccessPolicyToken(ctx context.Context, d *schema.ResourceData, m
client := meta.(*common.Client).GrafanaCloudAPI
region := d.Get("region").(string)

tokenInput := gapi.CreateCloudAccessPolicyTokenInput{
AccessPolicyID: d.Get("access_policy_id").(string),
tokenInput := gcom.PostTokensRequest{
AccessPolicyId: d.Get("access_policy_id").(string),
Name: d.Get("name").(string),
DisplayName: d.Get("display_name").(string),
DisplayName: common.Ref(d.Get("display_name").(string)),
}

if v, ok := d.GetOk("expires_at"); ok {
Expand All @@ -106,12 +106,13 @@ func CreateCloudAccessPolicyToken(ctx context.Context, d *schema.ResourceData, m
tokenInput.ExpiresAt = &expiresAt
}

result, err := client.CreateCloudAccessPolicyToken(region, tokenInput)
req := client.TokensAPI.PostTokens(ctx).Region(region).XRequestId(ClientRequestID()).PostTokensRequest(tokenInput)
result, _, err := req.Execute()
if err != nil {
return apiError(err)
}

d.SetId(fmt.Sprintf("%s/%s", region, result.ID))
d.SetId(fmt.Sprintf("%s/%s", region, *result.Id))
d.Set("token", result.Token)

return ReadCloudAccessPolicyToken(ctx, d, meta)
Expand All @@ -126,10 +127,10 @@ func UpdateCloudAccessPolicyToken(ctx context.Context, d *schema.ResourceData, m
displayName = d.Get("name").(string)
}

_, err := client.UpdateCloudAccessPolicyToken(region, id, gapi.UpdateCloudAccessPolicyTokenInput{
DisplayName: displayName,
req := client.TokensAPI.PostToken(ctx, id).Region(region).XRequestId(ClientRequestID()).PostTokenRequest(gcom.PostTokenRequest{
DisplayName: &displayName,
})
if err != nil {
if _, _, err := req.Execute(); err != nil {
return apiError(err)
}

Expand All @@ -141,12 +142,12 @@ func ReadCloudAccessPolicyToken(ctx context.Context, d *schema.ResourceData, met

region, id, _ := strings.Cut(d.Id(), "/")

result, err := client.CloudAccessPolicyTokenByID(region, id)
result, _, err := client.TokensAPI.GetToken(ctx, id).Region(region).Execute()
if err, shouldReturn := common.CheckReadError("policy token", d, err); shouldReturn {
return err
}

d.Set("access_policy_id", result.AccessPolicyID)
d.Set("access_policy_id", result.AccessPolicyId)
d.Set("region", region)
d.Set("name", result.Name)
d.Set("display_name", result.DisplayName)
Expand All @@ -165,5 +166,6 @@ func DeleteCloudAccessPolicyToken(ctx context.Context, d *schema.ResourceData, m
client := meta.(*common.Client).GrafanaCloudAPI
region, id, _ := strings.Cut(d.Id(), "/")

return apiError(client.DeleteCloudAccessPolicyToken(region, id))
_, _, err := client.TokensAPI.DeleteToken(ctx, id).Region(region).XRequestId(ClientRequestID()).Execute()
return apiError(err)
}
Loading

0 comments on commit ade9d4c

Please sign in to comment.