Skip to content

Commit

Permalink
Refactored logging and client configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
micahlmartin committed Jan 30, 2022
1 parent ac65e95 commit 46b61b8
Show file tree
Hide file tree
Showing 28 changed files with 439 additions and 465 deletions.
23 changes: 8 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,31 @@ go 1.16

require (
github.com/Microsoft/go-winio v0.5.1 // indirect
github.com/Microsoft/hcsshim v0.9.2 // indirect
github.com/antihax/optional v1.0.0
github.com/bits-and-blooms/bitset v1.2.1 // indirect
github.com/containerd/cgroups v1.0.2 // indirect
github.com/containerd/containerd v1.5.9 // indirect
github.com/containerd/continuity v0.2.2 // indirect
github.com/docker/docker v20.10.12+incompatible
github.com/docker/go-connections v0.4.0 // indirect
github.com/gogo/googleapis v1.4.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/gorilla/mux v1.7.3 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2
github.com/hashicorp/go-hclog v1.0.0 // indirect
github.com/hashicorp/go-retryablehttp v0.7.0
github.com/jinzhu/copier v0.3.5
github.com/klauspost/compress v1.14.2 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/opencontainers/image-spec v1.0.2
github.com/opencontainers/runc v1.1.0 // indirect
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.0
go.opencensus.io v0.23.0 // indirect
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220126215142-9970aeb2e350 // indirect
google.golang.org/grpc v1.44.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
149 changes: 19 additions & 130 deletions go.sum

Large diffs are not rendered by default.

65 changes: 0 additions & 65 deletions harness/api/client.go

This file was deleted.

57 changes: 24 additions & 33 deletions harness/cd/cac.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"errors"
"fmt"
"io"
"log"
"mime/multipart"
"path"
"strings"

"github.com/harness-io/harness-go-sdk/harness/cd/cac"
Expand Down Expand Up @@ -56,8 +56,9 @@ func FindConfigAsCodeItemByUUID(rootItem *cac.ConfigAsCodeItem, uuid string) *ca
}

func (c *ConfigAsCodeClient) GetDirectoryItemContent(restName string, uuid string, applicationId string) (*cac.ConfigAsCodeItem, error) {
path := fmt.Sprintf("/setup-as-code/yaml/%s/%s", restName, uuid)
log.Printf("[DEBUG] CAC: Getting directory item content at %s", path)
path := getCacPath(fmt.Sprintf("/%s/%s", restName, uuid))

c.ApiClient.Log.Debugf("Getting directory item content at %s", path)

req, err := c.ApiClient.NewAuthorizedGetRequest(path)

Expand All @@ -82,11 +83,14 @@ func (c *ConfigAsCodeClient) GetDirectoryItemContent(restName string, uuid strin
return item, nil
}

func getCacPath(apiPath string) string {
return path.Join(utils.DefaultCacApiUrl, apiPath)
}

func (c *ConfigAsCodeClient) GetDirectoryTree(applicationId string) (*cac.ConfigAsCodeItem, error) {
path := "/setup-as-code/yaml/directory"
log.Printf("[DEBUG] CAC: Getting directory tree for app '%s'", applicationId)
c.ApiClient.Log.Debugf("Getting directory tree for app '%s'", applicationId)

req, err := c.ApiClient.NewAuthorizedGetRequest(path)
req, err := c.ApiClient.NewAuthorizedGetRequest(getCacPath("/directory"))

if err != nil {
return nil, err
Expand Down Expand Up @@ -120,7 +124,7 @@ func (c *ConfigAsCodeClient) UpsertYamlEntity(filePath cac.YamlPath, entity inte
}

func (c *ConfigAsCodeClient) UpsertRawYaml(filePath cac.YamlPath, yaml []byte) (*cac.ConfigAsCodeItem, error) {
log.Printf("[DEBUG] CAC: Upserting yaml at %s", filePath)
c.ApiClient.Log.Debugf("Upserting yaml at %s", filePath)

// Setup form fields
var b bytes.Buffer
Expand All @@ -137,9 +141,7 @@ func (c *ConfigAsCodeClient) UpsertRawYaml(filePath cac.YamlPath, yaml []byte) (

w.Close()

log.Printf("[TRACE] CAC: HTTP Request Body %s", string(yaml))

req, err := c.ApiClient.NewAuthorizedPostRequest("/setup-as-code/yaml/upsert-entity", &b)
req, err := c.ApiClient.NewAuthorizedPostRequest(getCacPath("/upsert-entity"), &b)

// Set proper content header
req.Header.Set(helpers.HTTPHeaders.ContentType.String(), w.FormDataContentType())
Expand All @@ -164,8 +166,6 @@ func (c *ConfigAsCodeClient) UpsertRawYaml(filePath cac.YamlPath, yaml []byte) (

func (c *ConfigAsCodeClient) ExecuteRequest(request *retryablehttp.Request) (*cac.ConfigAsCodeItem, error) {

log.Printf("[TRACE] CAC: Request url %s", request.URL)

res, err := c.ApiClient.Configuration.HTTPClient.Do(request)
if err != nil {
return nil, err
Expand All @@ -183,10 +183,6 @@ func (c *ConfigAsCodeClient) ExecuteRequest(request *retryablehttp.Request) (*ca
return nil, fmt.Errorf("error reading body: %s", err)
}

// Check for request throttling
responseString := buf.String()
log.Printf("[TRACE] CAC: HTTP response %d - %s", res.StatusCode, responseString)

responseObj := &cac.Response{}

// Unmarshal into our response object
Expand Down Expand Up @@ -226,8 +222,8 @@ type ConfigAsCodeClient struct {
}

func (c *ConfigAsCodeClient) DeleteEntity(filePath cac.YamlPath) error {
log.Printf("[DEBUG] CAC: Deleting entity at %s", filePath)
req, err := c.ApiClient.NewAuthorizedDeleteRequest("/setup-as-code/yaml/delete-entities")
c.ApiClient.Log.Debugf("Deleting entity at %s", filePath)
req, err := c.ApiClient.NewAuthorizedDeleteRequest(getCacPath("/delete-entities"))

if err != nil {
return err
Expand All @@ -239,8 +235,6 @@ func (c *ConfigAsCodeClient) DeleteEntity(filePath cac.YamlPath) error {
q.Add(helpers.QueryParameters.FilePaths.String(), string(filePath))
req.URL.RawQuery = q.Encode()

log.Printf("[DEBUG] Url: %s", req.URL)

resp, err := c.ExecuteRequest(req)
if err != nil {
return err
Expand Down Expand Up @@ -268,14 +262,11 @@ func (c *ConfigAsCodeClient) UpsertObject(input interface{}, filePath cac.YamlPa
}

// Upsert the yaml document
resp, err := c.UpsertYamlEntity(filePath, input)
_, err := c.UpsertYamlEntity(filePath, input)
if err != nil {
return err
}

log.Printf("[TRACE] UUID: %s", resp.UUID)
log.Printf("[TRACE] EntityId: %s", resp.EntityId)

appId, ok := utils.TryGetFieldValue(input, "ApplicationId")
if !ok {
appId = ""
Expand All @@ -293,55 +284,55 @@ func (c *ConfigAsCodeClient) UpsertObject(input interface{}, filePath cac.YamlPa
// Typically this is needed just after an Upsert command. The Upsert API unfortunately does not
// return the Id of the newly created object.
func (c *ConfigAsCodeClient) FindObjectByPath(applicationId string, filePath cac.YamlPath, obj interface{}) error {
log.Printf("[DEBUG] CAC: Finding object by path %s", filePath)
c.ApiClient.Log.Debugf("Finding object by path %s", filePath)
rootItem, err := c.GetDirectoryTree(applicationId)
if err != nil {
return err
}

item := FindConfigAsCodeItemByPath(rootItem, filePath)
if item == nil {
log.Printf("unable to find item at `%s`", filePath)
c.ApiClient.Log.Debugf("unable to find item at `%s`", filePath)
return nil
}

return c.ParseObject(item, filePath, applicationId, obj)
}

func (c *ConfigAsCodeClient) FindYamlByPath(applicationId string, filePath cac.YamlPath) (*cac.YamlEntity, error) {
log.Printf("[DEBUG] CAC: Find yaml by path %s", filePath)
c.ApiClient.Log.Debugf("Find yaml by path %s", filePath)
rootItem, err := c.GetDirectoryTree(applicationId)
if err != nil {
return nil, err
}

item := FindConfigAsCodeItemByPath(rootItem, filePath)
if item == nil {
log.Printf("unable to find item at `%s`", filePath)
c.ApiClient.Log.Debugf("unable to find item at `%s`", filePath)
return nil, nil
}

return c.GetYamlDetails(item, filePath, applicationId)
}

func (c *ConfigAsCodeClient) FindObjectById(applicationId string, objectId string, out interface{}) error {
log.Printf("[DEBUG] CAC: Find object by id %s", objectId)
c.ApiClient.Log.Debugf("Find object by id %s", objectId)
rootItem, err := c.GetDirectoryTree(applicationId)
if err != nil {
return err
}

i := FindConfigAsCodeItemByUUID(rootItem, objectId)
if i == nil {
log.Printf("[DEBUG] cannot find obj with id: " + objectId)
c.ApiClient.Log.Debugf("cannot find obj with id: " + objectId)
return nil
}

return c.ParseObject(i, cac.YamlPath(i.DirectoryPath.Path), applicationId, out)
}

func (c *ConfigAsCodeClient) FindRootAccountObjectByName(name string) (*cac.ConfigAsCodeItem, error) {
log.Printf("[DEBUG] CAC: Finding account by name %s", name)
c.ApiClient.Log.Debugf("Finding account by name %s", name)
root, err := c.GetDirectoryTree("")
if err != nil || root == nil {
return root, err
Expand All @@ -366,7 +357,7 @@ func (c *ConfigAsCodeClient) GetTemplateLibraryRootPathName() (cac.YamlPath, err
}

func (c *ConfigAsCodeClient) GetYamlDetails(item *cac.ConfigAsCodeItem, filePath cac.YamlPath, applicationId string) (*cac.YamlEntity, error) {
log.Printf("[DEBUG] CAC: Get yaml details %s", filePath)
c.ApiClient.Log.Debugf("Get yaml details %s", filePath)
itemContent, err := c.GetDirectoryItemContent(item.RestName, item.UUID, applicationId)
if err != nil {
return nil, err
Expand All @@ -384,7 +375,7 @@ func (c *ConfigAsCodeClient) GetYamlDetails(item *cac.ConfigAsCodeItem, filePath
}

func (c *ConfigAsCodeClient) ParseObject(item *cac.ConfigAsCodeItem, filePath cac.YamlPath, applicationId string, obj interface{}) error {
log.Printf("[DEBUG] CAC: Parse yaml entity %s", filePath)
c.ApiClient.Log.Debugf("Parse yaml entity %s", filePath)
itemContent, err := c.GetDirectoryItemContent(item.RestName, item.UUID, applicationId)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 46b61b8

Please sign in to comment.