Skip to content

Commit

Permalink
Refactored CD api
Browse files Browse the repository at this point in the history
  • Loading branch information
micahlmartin committed Nov 10, 2021
1 parent 107645b commit ff6dbdb
Show file tree
Hide file tree
Showing 922 changed files with 606 additions and 598 deletions.
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
run:
skip-dirs:
- harness/api/nextgen
- harness/nextgen
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ go get github.com/harness-io/harness-go-sdk@latest

```go
client := NewClient()
app, err := client.Applications().GetApplicationByName("my-app)
app, err := client.ApplicationClient.GetApplicationByName("my-app)
```
### Create a Service
Expand All @@ -72,7 +72,7 @@ If you need to provide additional configuration you can create a client object f
```go
client := &Client{
UserAgent: getUserAgentString(),
Endpoint: utils.GetEnv(envvar.HarnessEndpoint, DefaultApiUrl),
Endpoint: utils.GetEnv(envvar.HarnessEndpoint, utils.DefaultApiUrl),
AccountId: os.Getenv(envvar.HarnessAccountId),
APIKey: os.Getenv(envvar.HarnessApiKey),
BearerToken: os.Getenv(envvar.HarnessBearerToken),
Expand Down
66 changes: 19 additions & 47 deletions harness/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,25 @@ import (
"log"
"net/http"
"os"
"strings"
"time"

"github.com/harness-io/harness-go-sdk/harness"
"github.com/harness-io/harness-go-sdk/harness/api/nextgen"
"github.com/harness-io/harness-go-sdk/harness/cd"
"github.com/harness-io/harness-go-sdk/harness/helpers"
"github.com/harness-io/harness-go-sdk/harness/nextgen"
"github.com/harness-io/harness-go-sdk/harness/utils"
"github.com/hashicorp/go-retryablehttp"
)

type Client struct {
AccountId string
APIKey string
BearerToken string
Endpoint string
HTTPClient *retryablehttp.Client
UserAgent string
NGClient *nextgen.APIClient
AccountId string
Endpoint string
NGClient *nextgen.APIClient
CDClient *cd.ApiClient
}

func NewClient() *Client {

httpClient := &retryablehttp.Client{
RetryMax: 10,
RetryWaitMin: 5 * time.Second,
Expand All @@ -36,15 +35,20 @@ func NewClient() *Client {
Backoff: retryablehttp.DefaultBackoff,
CheckRetry: retryablehttp.DefaultRetryPolicy,
}

userAgent := getUserAgentString()

return &Client{
AccountId: helpers.EnvVars.HarnessAccountId.Get(),
APIKey: helpers.EnvVars.HarnessApiKey.Get(),
BearerToken: helpers.EnvVars.HarnessBearerToken.Get(),
Endpoint: helpers.EnvVars.HarnessEndpoint.GetWithDefault(DefaultApiUrl),
UserAgent: userAgent,
HTTPClient: httpClient,
AccountId: helpers.EnvVars.HarnessAccountId.Get(),
Endpoint: helpers.EnvVars.HarnessEndpoint.GetWithDefault(utils.DefaultApiUrl),
CDClient: cd.NewClient(&cd.Configuration{
AccountId: helpers.EnvVars.HarnessAccountId.Get(),
APIKey: helpers.EnvVars.HarnessApiKey.Get(),
BearerToken: helpers.EnvVars.HarnessBearerToken.Get(),
Endpoint: helpers.EnvVars.HarnessEndpoint.GetWithDefault(utils.DefaultApiUrl),
UserAgent: userAgent,
HTTPClient: httpClient,
}),
NGClient: nextgen.NewAPIClient(&nextgen.Configuration{
BasePath: helpers.EnvVars.HarnessNGEndpoint.GetWithDefault(DefaultNGApiUrl),
DefaultHeader: map[string]string{
Expand All @@ -56,38 +60,6 @@ func NewClient() *Client {
}
}

func (client *Client) NewAuthorizedGetRequest(path string) (*retryablehttp.Request, error) {
return client.NewAuthorizedRequest(path, http.MethodGet, nil)
}

func (client *Client) NewAuthorizedPostRequest(path string, rawBody interface{}) (*retryablehttp.Request, error) {
return client.NewAuthorizedRequest(path, http.MethodPost, rawBody)
}

func (client *Client) NewAuthorizedDeleteRequest(path string) (*retryablehttp.Request, error) {
return client.NewAuthorizedRequest(path, http.MethodDelete, nil)
}

func (client *Client) NewAuthorizedRequest(path string, method string, rawBody interface{}) (*retryablehttp.Request, error) {
url := strings.Join([]string{client.Endpoint, path}, "")
req, err := retryablehttp.NewRequest(method, url, rawBody)

if err != nil {
return nil, err
}

req.Header.Set(helpers.HTTPHeaders.UserAgent.String(), client.UserAgent)
req.Header.Set(helpers.HTTPHeaders.ContentType.String(), helpers.HTTPHeaders.ApplicationJson.String())
req.Header.Set(helpers.HTTPHeaders.Accept.String(), helpers.HTTPHeaders.ApplicationJson.String())
req.Header.Set(helpers.HTTPHeaders.ApiKey.String(), client.APIKey)

if client.BearerToken != "" {
req.Header.Set(helpers.HTTPHeaders.Authorization.String(), fmt.Sprintf("Bearer %s", client.BearerToken))
}

return req, err
}

func getUserAgentString() string {
return fmt.Sprintf("%s-%s", harness.SDKName, harness.SDKVersion)
}
1 change: 0 additions & 1 deletion harness/api/cloudprovider_test.go

This file was deleted.

2 changes: 0 additions & 2 deletions harness/api/consts.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package api

const (
DefaultApiUrl = "https://app.harness.io"
DefaultGraphQLApiUrl = "/gateway/api/graphql"
GraphQLInvalidTokenErrorCode = "INVALID_TOKEN"
DefaultNGApiUrl = "https://app.harness.io/gateway/ng/api"
)
1 change: 0 additions & 1 deletion harness/api/nextgen/.swagger-codegen/VERSION

This file was deleted.

29 changes: 11 additions & 18 deletions harness/api/application.go → harness/cd/application.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
package api
package cd

import (
"fmt"
"strings"

"github.com/harness-io/harness-go-sdk/harness/api/graphql"
"github.com/harness-io/harness-go-sdk/harness/cd/graphql"
)

// Helper type for accessing all application related crud methods
type ApplicationClient struct {
APIClient *Client
}

// Get the client for interacting with Harness Applications
func (c *Client) Applications() *ApplicationClient {
return &ApplicationClient{
APIClient: c,
}
ApiClient *ApiClient
}

// CRUD
Expand All @@ -35,12 +28,12 @@ func (ac *ApplicationClient) GetApplicationById(id string) (*graphql.Application
res := struct {
Application graphql.Application
}{}
err := ac.APIClient.ExecuteGraphQLQuery(query, &res)
err := ac.ApiClient.ExecuteGraphQLQuery(query, &res)

// Need to fix https://harness.atlassian.net/browse/PL-19934
if err != nil {
if strings.Contains(err.Error(), "User not authorized") {
if cacApp, err := ac.APIClient.ConfigAsCode().GetApplicationById(id); err != nil {
if cacApp, err := ac.ApiClient.ConfigAsCodeClient.GetApplicationById(id); err != nil {
return nil, err
} else if cacApp.IsEmpty() {
return nil, nil
Expand All @@ -67,12 +60,12 @@ func (ac *ApplicationClient) GetApplicationByName(name string) (*graphql.Applica
res := &struct {
ApplicationByName graphql.Application
}{}
err := ac.APIClient.ExecuteGraphQLQuery(query, &res)
err := ac.ApiClient.ExecuteGraphQLQuery(query, &res)

// Need to fix https://harness.atlassian.net/browse/PL-19934
if err != nil {
// if strings.Contains(err.Error(), "User not authorized") {
// if cacApp, err := ac.APIClient.ConfigAsCode().GetApplicationByName(name); err != nil {
// if cacApp, err := ac.APIClient.ConfigAsCodeClient.GetApplicationByName(name); err != nil {
// return nil, err
// } else if cacApp == nil {
// return nil, nil
Expand Down Expand Up @@ -105,7 +98,7 @@ func (ac *ApplicationClient) CreateApplication(input *graphql.Application) (*gra
res := &struct {
CreateApplication graphql.CreateApplicationPayload
}{}
err := ac.APIClient.ExecuteGraphQLQuery(query, &res)
err := ac.ApiClient.ExecuteGraphQLQuery(query, &res)

if err != nil {
return nil, err
Expand All @@ -129,7 +122,7 @@ func (ac *ApplicationClient) DeleteApplication(id string) error {
},
}

err := ac.APIClient.ExecuteGraphQLQuery(query, &struct{}{})
err := ac.ApiClient.ExecuteGraphQLQuery(query, &struct{}{})

return err
}
Expand All @@ -154,7 +147,7 @@ func (ac *ApplicationClient) UpdateApplication(input *graphql.UpdateApplicationI
UpdateApplication graphql.UpdateApplicationPayload
}{}

err := ac.APIClient.ExecuteGraphQLQuery(query, &res)
err := ac.ApiClient.ExecuteGraphQLQuery(query, &res)

if err != nil {
return nil, err
Expand Down Expand Up @@ -182,7 +175,7 @@ func (ac *ApplicationClient) ListApplications(limit int, offset int) ([]*graphql
}
}{}

err := ac.APIClient.ExecuteGraphQLQuery(query, &res)
err := ac.ApiClient.ExecuteGraphQLQuery(query, &res)

if err != nil {
return nil, nil, err
Expand Down
28 changes: 14 additions & 14 deletions harness/api/application_test.go → harness/cd/application_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package api
package cd

import (
"fmt"
"testing"
"time"

"github.com/harness-io/harness-go-sdk/harness/api/graphql"
"github.com/harness-io/harness-go-sdk/harness/cd/graphql"
"github.com/harness-io/harness-go-sdk/harness/utils"
"github.com/stretchr/testify/require"
)
Expand All @@ -18,7 +18,7 @@ func TestGetApplicationById(t *testing.T) {

// Lookup newly created app by ID
client := getClient()
app, err := client.Applications().GetApplicationById(newApp.Id)
app, err := client.ApplicationClient.GetApplicationById(newApp.Id)

// Verify
require.NoError(t, err)
Expand All @@ -28,7 +28,7 @@ func TestGetApplicationById(t *testing.T) {
require.Equal(t, newApp.Description, app.Description, "Test application description")

// cleanup
err = client.Applications().DeleteApplication(newApp.Id)
err = client.ApplicationClient.DeleteApplication(newApp.Id)
require.Nil(t, err, "Failed to delete application: %s", err)
}

Expand All @@ -39,15 +39,15 @@ func TestGetApplicationByName(t *testing.T) {
require.NoError(t, err, "Failed to create application: %s", err)

client := getClient()
app, err := client.Applications().GetApplicationByName(newApp.Name)
app, err := client.ApplicationClient.GetApplicationByName(newApp.Name)

// Verify
require.NoError(t, err, "Could not look up application by name")
require.NotNil(t, app, "App not found")
require.Equal(t, newApp.Name, app.Name, "App name doesn't match")

// cleanup
err = client.Applications().DeleteApplication(newApp.Id)
err = client.ApplicationClient.DeleteApplication(newApp.Id)
require.Nil(t, err, "Failed to delete application: %s", err)
}

Expand All @@ -62,15 +62,15 @@ func TestCreateApplication(t *testing.T) {
Description: "Test application description",
}

app, err := client.Applications().CreateApplication(input)
app, err := client.ApplicationClient.CreateApplication(input)

// Verify
require.NoError(t, err, "Failed to create application: %s", err)
require.NotNil(t, app, "Application should not be nil")
require.Equal(t, input.Name, app.Name, "Application name doesn't match")

// Cleanup
err = client.Applications().DeleteApplication(app.Id)
err = client.ApplicationClient.DeleteApplication(app.Id)
require.Nil(t, err, "Failed to delete application: %s", err)
}

Expand All @@ -84,11 +84,11 @@ func TestGetDeletedApplication(t *testing.T) {
client := getClient()

// Delete application
err = client.Applications().DeleteApplication(newApp.Id)
err = client.ApplicationClient.DeleteApplication(newApp.Id)
require.Nil(t, err, "Failed to delete application: %s", err)

// Verify
app, err := client.Applications().GetApplicationById(newApp.Id)
app, err := client.ApplicationClient.GetApplicationById(newApp.Id)

require.Nil(t, app, "Failed to delete app")
require.NoError(t, err)
Expand All @@ -109,7 +109,7 @@ func TestUpdateApplication(t *testing.T) {
client := getClient()

// Update application
updatedApp, err := client.Applications().UpdateApplication(&graphql.UpdateApplicationInput{
updatedApp, err := client.ApplicationClient.UpdateApplication(&graphql.UpdateApplicationInput{
ApplicationId: newApp.Id,
Name: expectedName,
})
Expand All @@ -120,7 +120,7 @@ func TestUpdateApplication(t *testing.T) {
require.Equal(t, expectedName, updatedApp.Name)

// Cleanup
err = client.Applications().DeleteApplication(updatedApp.Id)
err = client.ApplicationClient.DeleteApplication(updatedApp.Id)
require.Nil(t, err, "Failed to delete application: %s", err)
}

Expand All @@ -133,7 +133,7 @@ func createApplication(name string) (*graphql.Application, error) {
Description: "Test application description",
}

return client.Applications().CreateApplication(input)
return client.ApplicationClient.CreateApplication(input)
}

func TestListApplications(t *testing.T) {
Expand All @@ -143,7 +143,7 @@ func TestListApplications(t *testing.T) {
hasMore := true

for hasMore {
apps, pagination, err := client.Applications().ListApplications(limit, offset)
apps, pagination, err := client.ApplicationClient.ListApplications(limit, offset)
require.NoError(t, err, "Failed to list applications: %s", err)
require.NotEmpty(t, apps, "No applications found")
require.NotNil(t, pagination, "Pagination should not be nil")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package api
package cd

import (
"fmt"

"github.com/harness-io/harness-go-sdk/harness/api/graphql"
"github.com/harness-io/harness-go-sdk/harness/cd/graphql"
)

func (c *CloudProviderClient) GetAwsCloudProviderById(Id string) (*graphql.AwsCloudProvider, error) {
Expand Down
Loading

0 comments on commit ff6dbdb

Please sign in to comment.