Skip to content

Commit

Permalink
Cleanup logging
Browse files Browse the repository at this point in the history
  • Loading branch information
micahlmartin committed Oct 19, 2021
1 parent 0ef38cd commit 5c6833a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
24 changes: 17 additions & 7 deletions harness/api/cac.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ func FindConfigAsCodeItemByUUID(rootItem *cac.ConfigAsCodeItem, uuid string) *ca

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

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

if err != nil {
Expand All @@ -79,6 +81,8 @@ func (c *ConfigAsCodeClient) GetDirectoryItemContent(restName string, uuid strin

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

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

if err != nil {
Expand All @@ -104,7 +108,6 @@ func (c *ConfigAsCodeClient) GetDirectoryTree(applicationId string) (*cac.Config
}

func (c *ConfigAsCodeClient) UpsertYamlEntity(filePath cac.YamlPath, entity interface{}) (*cac.ConfigAsCodeItem, error) {

payload, err := yaml.Marshal(&entity)
if err != nil {
return nil, err
Expand All @@ -114,6 +117,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)

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

w.Close()

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

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

Expand All @@ -157,7 +161,7 @@ func (c *ConfigAsCodeClient) UpsertRawYaml(filePath cac.YamlPath, yaml []byte) (

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

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

res, err := c.ApiClient.HTTPClient.Do(request)
if err != nil {
Expand All @@ -178,7 +182,7 @@ func (c *ConfigAsCodeClient) ExecuteRequest(request *retryablehttp.Request) (*ca

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

responseObj := &cac.Response{}

Expand Down Expand Up @@ -219,6 +223,7 @@ 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("/gateway/api/setup-as-code/yaml/delete-entities")

if err != nil {
Expand All @@ -232,7 +237,6 @@ func (c *ConfigAsCodeClient) DeleteEntity(filePath cac.YamlPath) error {
req.URL.RawQuery = q.Encode()

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

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

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

appId, ok := utils.TryGetFieldValue(input, "ApplicationId")
if !ok {
Expand All @@ -286,6 +290,7 @@ 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)
rootItem, err := c.GetDirectoryTree(applicationId)
if err != nil {
return err
Expand All @@ -301,6 +306,7 @@ func (c *ConfigAsCodeClient) FindObjectByPath(applicationId string, filePath cac
}

func (c *ConfigAsCodeClient) FindYamlByPath(applicationId string, filePath cac.YamlPath) (*cac.YamlEntity, error) {
log.Printf("[DEBUG] CAC: Find yaml by path %s", filePath)
rootItem, err := c.GetDirectoryTree(applicationId)
if err != nil {
return nil, err
Expand All @@ -316,6 +322,7 @@ func (c *ConfigAsCodeClient) FindYamlByPath(applicationId string, filePath cac.Y
}

func (c *ConfigAsCodeClient) FindObjectById(applicationId string, objectId string, out interface{}) error {
log.Printf("[DEBUG] CAC: Find object by id %s", objectId)
rootItem, err := c.GetDirectoryTree(applicationId)
if err != nil {
return err
Expand All @@ -331,6 +338,7 @@ func (c *ConfigAsCodeClient) FindObjectById(applicationId string, objectId strin
}

func (c *ConfigAsCodeClient) FindRootAccountObjectByName(name string) (*cac.ConfigAsCodeItem, error) {
log.Printf("[DEBUG] CAC: Finding account by name %s", name)
root, err := c.GetDirectoryTree("")
if err != nil || root == nil {
return root, err
Expand All @@ -355,6 +363,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)
itemContent, err := c.GetDirectoryItemContent(item.RestName, item.UUID, applicationId)
if err != nil {
return nil, err
Expand All @@ -372,6 +381,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: Prase yaml entity %s", filePath)
itemContent, err := c.GetDirectoryItemContent(item.RestName, item.UUID, applicationId)
if err != nil {
return err
Expand Down
9 changes: 9 additions & 0 deletions harness/api/cac_cloudprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

func (c *ConfigAsCodeClient) UpsertSpotInstCloudProvider(input *cac.SpotInstCloudProvider) (*cac.SpotInstCloudProvider, error) {
log.Printf("[DEBUG] CAC: Upsert Spot cloud provider %s", input.Name)
out := &cac.SpotInstCloudProvider{}
err := c.UpsertCloudProvider(input, out)
if err != nil {
Expand All @@ -19,6 +20,7 @@ func (c *ConfigAsCodeClient) UpsertSpotInstCloudProvider(input *cac.SpotInstClou
}

func (c *ConfigAsCodeClient) UpsertPcfCloudProvider(input *cac.PcfCloudProvider) (*cac.PcfCloudProvider, error) {
log.Printf("[DEBUG] CAC: Upsert PCF cloud provider %s", input.Name)
out := &cac.PcfCloudProvider{}
err := c.UpsertCloudProvider(input, out)
if err != nil {
Expand All @@ -29,6 +31,7 @@ func (c *ConfigAsCodeClient) UpsertPcfCloudProvider(input *cac.PcfCloudProvider)
}

func (c *ConfigAsCodeClient) UpsertKubernetesCloudProvider(input *cac.KubernetesCloudProvider) (*cac.KubernetesCloudProvider, error) {
log.Printf("[DEBUG] CAC: Upsert Kubernetes cloud provider %s", input.Name)
out := &cac.KubernetesCloudProvider{}
err := c.UpsertCloudProvider(input, out)
if err != nil {
Expand All @@ -39,6 +42,7 @@ func (c *ConfigAsCodeClient) UpsertKubernetesCloudProvider(input *cac.Kubernetes
}

func (c *ConfigAsCodeClient) UpsertAzureCloudProvider(input *cac.AzureCloudProvider) (*cac.AzureCloudProvider, error) {
log.Printf("[DEBUG] CAC: Upsert Azure cloud provider %s", input.Name)
out := &cac.AzureCloudProvider{}
err := c.UpsertCloudProvider(input, out)
if err != nil {
Expand All @@ -49,6 +53,7 @@ func (c *ConfigAsCodeClient) UpsertAzureCloudProvider(input *cac.AzureCloudProvi
}

func (c *ConfigAsCodeClient) UpsertGcpCloudProvider(input *cac.GcpCloudProvider) (*cac.GcpCloudProvider, error) {
log.Printf("[DEBUG] CAC: Upsert GCP cloud provider %s", input.Name)
out := &cac.GcpCloudProvider{}
err := c.UpsertCloudProvider(input, out)
if err != nil {
Expand All @@ -59,6 +64,7 @@ func (c *ConfigAsCodeClient) UpsertGcpCloudProvider(input *cac.GcpCloudProvider)
}

func (c *ConfigAsCodeClient) UpsertPhysicalDataCenterCloudProvider(input *cac.PhysicalDatacenterCloudProvider) (*cac.PhysicalDatacenterCloudProvider, error) {
log.Printf("[DEBUG] CAC: Upsert Datacenter cloud provider %s", input.Name)
out := &cac.PhysicalDatacenterCloudProvider{}
err := c.UpsertCloudProvider(input, out)
if err != nil {
Expand All @@ -69,6 +75,7 @@ func (c *ConfigAsCodeClient) UpsertPhysicalDataCenterCloudProvider(input *cac.Ph
}

func (c *ConfigAsCodeClient) UpsertAwsCloudProvider(input *cac.AwsCloudProvider) (*cac.AwsCloudProvider, error) {
log.Printf("[DEBUG] CAC: Upsert AWS cloud provider %s", input.Name)
out := &cac.AwsCloudProvider{}
err := c.UpsertCloudProvider(input, out)
if err != nil {
Expand Down Expand Up @@ -98,6 +105,7 @@ func (c *ConfigAsCodeClient) UpsertCloudProvider(input interface{}, output inter
}

func (c *ConfigAsCodeClient) GetCloudProviderById(providerId string, out interface{}) error {
log.Printf("[DEBUG] CAC: Get cloud provider by id %s", providerId)
rootItem, err := c.GetDirectoryTree("")
if err != nil {
return err
Expand All @@ -113,6 +121,7 @@ func (c *ConfigAsCodeClient) GetCloudProviderById(providerId string, out interfa
}

func (c *ConfigAsCodeClient) GetCloudProviderByName(name string, obj interface{}) error {
log.Printf("[DEBUG] CAC: Get cloud provider by name %s", name)
filePath := cac.GetCloudProviderYamlPath(name)
return c.FindObjectByPath("", filePath, obj)
}
Expand Down
6 changes: 5 additions & 1 deletion harness/api/cac_infra_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package api

import (
"fmt"
"log"
"reflect"

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

func (c *ConfigAsCodeClient) UpsertInfraDefinition(input *cac.InfrastructureDefinition) (*cac.InfrastructureDefinition, error) {

log.Printf("[DEBUG] CAC: Upsert infra definition %s", input.Name)
if ok, err := input.Validate(); !ok {
return nil, err
}
Expand Down Expand Up @@ -44,6 +45,7 @@ func (c *ConfigAsCodeClient) UpsertInfraDefinition(input *cac.InfrastructureDefi
}

func (c *ConfigAsCodeClient) GetInfraDefinitionById(appId string, envId string, infraId string) (*cac.InfrastructureDefinition, error) {
log.Printf("[DEBUG] CAC: Get infra definition by id %s", infraId)
app, err := c.ApiClient.Applications().GetApplicationById(appId)
if err != nil {
return nil, err
Expand Down Expand Up @@ -78,6 +80,7 @@ func (c *ConfigAsCodeClient) GetInfraDefinitionById(appId string, envId string,
}

func (c *ConfigAsCodeClient) GetInfraDefinitionByName(appId string, envId string, infraName string) (*cac.InfrastructureDefinition, error) {
log.Printf("[DEBUG] CAC: Get infra definition by name %s", infraName)
app, err := c.ApiClient.Applications().GetApplicationById(appId)
if err != nil {
return nil, err
Expand Down Expand Up @@ -108,6 +111,7 @@ func (c *ConfigAsCodeClient) GetInfraDefinitionByName(appId string, envId string
}

func (c *ConfigAsCodeClient) DeleteInfraDefinition(applicationId string, environmentId string, infraId string) error {
log.Printf("[DEBUG] CAC: Delete infra definition %s", infraId)
app, err := c.ApiClient.Applications().GetApplicationById(applicationId)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions harness/api/cac_infra_definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ func TestCreateInfraDefinition_KubernetesDirect_Helm(t *testing.T) {
err = c.ConfigAsCode().DeleteInfraDefinition(infraDef.ApplicationId, infraDef.EnvironmentId, infraDef.Id)
require.NoError(t, err)

id, err := c.ConfigAsCode().GetInfraDefinitionById(infraDef.ApplicationId, infraDef.EnvironmentId, infraDef.Id)
require.NoError(t, err)
require.Nil(t, id)

err = c.Applications().DeleteApplication(app.Id)
require.NoError(t, err)

Expand Down
4 changes: 2 additions & 2 deletions harness/api/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (client *Client) NewGraphQLRequest(query *GraphQLQuery) (*retryablehttp.Req
return nil, err
}

log.Printf("[DEBUG] GraphQL Query: %s", requestBody.String())
log.Printf("[DEBUG] GraphQL: Query %s", requestBody.String())

req, err := client.NewAuthorizedPostRequest(DefaultGraphQLApiUrl, &requestBody)

Expand Down Expand Up @@ -78,7 +78,7 @@ func (client *Client) ExecuteGraphQLQuery(query *GraphQLQuery, responseObj inter
return fmt.Errorf("error reading body: %s", err)
}

log.Printf("[DEBUG] GraphQL response: %s", buf.String())
log.Printf("[TRACE] GraphQL response: %s", buf.String())

gqlResponse := &GraphQLStandardResponse{}

Expand Down

0 comments on commit 5c6833a

Please sign in to comment.