Skip to content

Commit

Permalink
Merge pull request #21 from atlanhq/Client-Refactoring-logging
Browse files Browse the repository at this point in the history
DVX 307 : Implement logging using `slog`
  • Loading branch information
0xquark authored May 7, 2024
2 parents 13a75f2 + e90dca7 commit 04b96d2
Show file tree
Hide file tree
Showing 10 changed files with 397 additions and 128 deletions.
28 changes: 28 additions & 0 deletions atlan/client/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,7 @@ func RetrieveMinimal(guid string) (*Assets2.Asset, error) {
queryParams["min_ext_info"] = "true"
queryParams["ignore_relationships"] = "true"

DefaultAtlanClient.logger.Debugf("Retrieving Asset with GUID: %s", guid)
response, err := DefaultAtlanClient.CallAPI(api, queryParams, nil)
if err != nil {
return nil, err
Expand All @@ -768,6 +769,8 @@ func RetrieveMinimal(guid string) (*Assets2.Asset, error) {
return nil, fmt.Errorf("error unmarshalling asset response: %v", err)
}

DefaultAtlanClient.logger.Debugf("Asset retrieved successfully with GUID: %s", guid)

api.Path = originalPath // Reset the api.Path to its original value
return &assetresponse, nil
}
Expand All @@ -790,16 +793,22 @@ func PurgeByGuid(guids []string) (*model.AssetMutationResponse, error) {
// Add the comma-separated string of GUIDs to the query parameters
queryParams["guid"] = guidString

DefaultAtlanClient.logger.Debugf("Purging assets with GUIDs: %v", guids)

// Call the API
resp, err := DefaultAtlanClient.CallAPI(api, queryParams, nil)
if err != nil {
DefaultAtlanClient.logger.Errorf("Error purging assets: %v", err)
return nil, err
}

DefaultAtlanClient.logger.Debugf("Assets purged successfully")

// Unmarshal the response into the AssetMutationResponse struct
var response model.AssetMutationResponse
err = json.Unmarshal(resp, &response)
if err != nil {
DefaultAtlanClient.logger.Errorf("Error unmarshalling response: %v", err)
return nil, fmt.Errorf("error unmarshaling response: %v", err)
}

Expand All @@ -813,13 +822,16 @@ func DeleteByGuid(guids []string) (*model.AssetMutationResponse, error) {
}

for _, guid := range guids {
DefaultAtlanClient.logger.Debugf("Deleting asset with GUID: %s", guid)
asset, err := RetrieveMinimal(guid)
if err != nil {
DefaultAtlanClient.logger.Errorf("Error retrieving asset: %v", err)
return nil, fmt.Errorf("error retrieving asset: %v", err)
}

// Assuming the asset has a CanBeArchived field that indicates if it can be archived
if *asset.TypeName == "AtlasGlossaryCategory" {
DefaultAtlanClient.logger.Errorf("Asset %s of type %s cannot be archived", guid, *asset.TypeName)
return nil, fmt.Errorf("asset %s of type %s cannot be archived", guid, *asset.TypeName)
}
}
Expand All @@ -837,23 +849,31 @@ func DeleteByGuid(guids []string) (*model.AssetMutationResponse, error) {
queryParams["guid"] = guidString

fmt.Println("Query Params:", queryParams)

DefaultAtlanClient.logger.Debugf("Soft deleting assets with GUIDs: %v", guids)

// Call the API
resp, err := DefaultAtlanClient.CallAPI(api, queryParams, nil)
if err != nil {
DefaultAtlanClient.logger.Errorf("Error soft deleting assets: %v", err)
return nil, err
}

DefaultAtlanClient.logger.Debugf("Assets soft deleted successfully")

// Unmarshal the response into the AssetMutationResponse struct
var response model.AssetMutationResponse
err = json.Unmarshal(resp, &response)
if err != nil {
DefaultAtlanClient.logger.Errorf("Error unmarshalling response: %v", err)
return nil, fmt.Errorf("error unmarshaling response: %v", err)
}

// Wait until each asset is deleted
for _, guid := range guids {
err = WaitTillDeleted(guid)
if err != nil {
DefaultAtlanClient.logger.Errorf("Error waiting for asset deletion: %v", err)
return nil, err
}
}
Expand All @@ -863,16 +883,20 @@ func DeleteByGuid(guids []string) (*model.AssetMutationResponse, error) {

// WaitTillDeleted waits for an asset to be deleted.
func WaitTillDeleted(guid string) error {
DefaultAtlanClient.logger.Debugf("Waiting for asset with GUID %s to be deleted", guid)
for i := 0; i < MaxRetries; i++ {
asset, err := RetrieveMinimal(guid)
if err != nil {
DefaultAtlanClient.logger.Errorf("Error retrieving asset: %v", err)
return fmt.Errorf("error retrieving asset: %v", err)
}

if *asset.Status == "DELETED" {
DefaultAtlanClient.logger.Debugf("Asset with GUID %s is deleted", guid)
return nil
}

DefaultAtlanClient.logger.Errorf("Retry limit overrun waiting for asset with GUID %s to be deleted", guid)
// If the asset is not deleted, wait for a while before retrying
time.Sleep(RetryInterval)
}
Expand All @@ -887,6 +911,7 @@ type SaveRequest struct {

// Save saves the assets in memory to the Atlas server.
func Save(assets ...AtlanObject) (*model.AssetMutationResponse, error) {
DefaultAtlanClient.logger.Debugf("Saving assets: %v", assets)
request := SaveRequest{
Entities: assets,
}
Expand All @@ -897,9 +922,12 @@ func Save(assets ...AtlanObject) (*model.AssetMutationResponse, error) {
return nil, err
}

DefaultAtlanClient.logger.Debugf("Assets saved successfully")

var response model.AssetMutationResponse
err = json.Unmarshal(resp, &response)
if err != nil {
DefaultAtlanClient.logger.Errorf("Error unmarshalling response: %v", err)
return nil, err
}

Expand Down
3 changes: 0 additions & 3 deletions atlan/client/atlan_tag_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ func TestIntegrationAtlanTagCache_RefreshCache(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
LoggingEnabled = false
client := NewContext()
cache := NewAtlanTagCache(client)

Expand All @@ -27,7 +26,6 @@ func TestIntegrationAtlanTagCache_GetIDForName(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
LoggingEnabled = false

client := NewContext()
cache := NewAtlanTagCache(client)
Expand All @@ -54,7 +52,6 @@ func TestIntegrationAtlanTagCache_GetNameForID(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
LoggingEnabled = false
client := NewContext()
cache := NewAtlanTagCache(client)

Expand Down
Loading

0 comments on commit 04b96d2

Please sign in to comment.