Skip to content

Commit

Permalink
Merge pull request #32 from atlanhq/dvx473-generic-method-for-assets
Browse files Browse the repository at this point in the history
DVX:473 Generic method for retrieval of assets
  • Loading branch information
0xquark authored May 31, 2024
2 parents 56e6065 + 985f4e6 commit a993489
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 243 deletions.
38 changes: 37 additions & 1 deletion atlan/client/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ import (
"github.com/atlanhq/atlan-go/atlan/model"
Assets2 "github.com/atlanhq/atlan-go/atlan/model/assets"
"hash/fnv"
"reflect"
"strings"
"time"
)

// AtlanObject is an interface that all asset types should implement
type AtlanObject interface {
MarshalJSON() ([]byte, error)
MarshalJSON() ([]byte, error) // Used for CRUD operation in Assets
}

// Asset is an interface that all asset types should implement
type Asset interface {
FromJSON(data []byte) error // Used for Retrieval of an Asset using GUID
}

// SearchAssets Struct to represent assets for searching
Expand Down Expand Up @@ -740,6 +747,35 @@ func NewSearchView() *ViewFields {

// Methods on assets

// GetbyGuid retrieves an asset by guid
func GetByGuid[T Asset](guid string) (T, error) {

var asset T

if DefaultAtlanClient == nil {
return asset, fmt.Errorf("default AtlanClient not initialized")
}

api := &GET_ENTITY_BY_GUID
api.Path += guid

response, err := DefaultAtlanClient.CallAPI(api, nil, nil)
if err != nil {
return asset, err
}

// Create a new instance of T using reflection
assetType := reflect.TypeOf(asset).Elem()
newAsset := reflect.New(assetType).Interface().(T)

err = newAsset.FromJSON(response)
if err != nil {
return asset, err
}

return newAsset, nil
}

// RetrieveMinimal retrieves an asset by its GUID, without any of its relationships.
func RetrieveMinimal(guid string) (*Assets2.Asset, error) {
if DefaultAtlanClient == nil {
Expand Down
45 changes: 0 additions & 45 deletions atlan/client/glossary_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package client
import (
"encoding/json"
"errors"
"fmt"
"github.com/atlanhq/atlan-go/atlan"
"github.com/atlanhq/atlan-go/atlan/model/assets"
"time"
Expand All @@ -26,50 +25,6 @@ func NewGlossaryClient(ac *AtlanClient) *GlossaryClient {
return &GlossaryClient{client: ac}
}

// GetGlossaryByGuid retrieves a glossary by its GUID.
func GetGlossaryByGuid(glossaryGuid string) (*assets.AtlasGlossary, error) {
if DefaultAtlanClient == nil {
return nil, fmt.Errorf("default AtlanClient not initialized")
}

api := &GET_ENTITY_BY_GUID
api.Path += glossaryGuid

response, err := DefaultAtlanClient.CallAPI(api, nil, nil)
if err != nil {
return nil, err
}

g, err := assets.FromJSON(response)
if err != nil {
return nil, err
}

return g, nil
}

// GetGlossaryTermByGuid retrieves a glossary term by its GUID.
func GetGlossaryTermByGuid(glossaryGuid string) (*assets.AtlasGlossaryTerm, error) {
if DefaultAtlanClient == nil {
return nil, fmt.Errorf("default AtlanClient not initialized")
}

api := &GET_ENTITY_BY_GUID
api.Path += glossaryGuid

response, err := DefaultAtlanClient.CallAPI(api, nil, nil)
if err != nil {
return nil, err
}

gt, err := assets.FromJSONTerm(response)
if err != nil {
return nil, err
}

return gt, nil
}

// Creator is used to create a new glossary asset in memory.
func (g *AtlasGlossary) Creator(name string, icon atlan.AtlanIcon) {
g.TypeName = assets.StringPtr("AtlasGlossary")
Expand Down
3 changes: 2 additions & 1 deletion atlan/client/glossary_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client
import (
"fmt"
"github.com/atlanhq/atlan-go/atlan"
"github.com/atlanhq/atlan-go/atlan/model/assets"
"github.com/stretchr/testify/assert"
"testing"
)
Expand Down Expand Up @@ -58,7 +59,7 @@ func testUpdateGlossary(t *testing.T, glossaryGUID string) {
}

func testRetrieveGlossary(t *testing.T, glossaryGUID string) {
glossary, err := GetGlossaryByGuid(glossaryGUID)
glossary, err := GetByGuid[*assets.AtlasGlossary](glossaryGUID)
if err != nil {
fmt.Println("Error:", err)
}
Expand Down
13 changes: 11 additions & 2 deletions atlan/model/assets/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ type CertificateStatus string

type PopularityInsights int

type Link string

type MCIncident string

type MCMonitor string
Expand Down Expand Up @@ -453,6 +451,17 @@ type AtlanTag struct {
RestrictPropagationThroughLineage bool `json:"restrictPropagationThroughLineage"`
}

type Link struct {
Guid string `json:"guid"`
TypeName string `json:"typeName"`
EntityStatus string `json:"entityStatus"`
DisplayText string `json:"displayText"`
RelationshipType string `json:"relationshipType"`
RelationshipGuid string `json:"relationshipGuid"`
RelationshipStatus string `json:"relationshipStatus"`
RelationshipAttributes map[string]interface{} `json:"relationshipAttributes"`
}

func StringPtr(s string) *string {
return &s
}
186 changes: 2 additions & 184 deletions atlan/model/assets/glossary.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,188 +89,6 @@ func (ag *AtlasGlossary) ToJSON() ([]byte, error) {
return json.MarshalIndent(ag, "", " ")
}

func FromJSON(data []byte) (*AtlasGlossary, error) {
var glossaryResponse AtlasGlossary
//fmt.Println("data")
//fmt.Println(string(data))
err := json.Unmarshal(data, &glossaryResponse)

return &glossaryResponse, err
}

// Glossary represents a glossary in Atlan.
type Glossary struct {
TypeName string `json:"typeName,omitempty"`
Attributes GlossaryAttributes `json:"attributes,omitempty"`
Guid string `json:"guid,omitempty"`
IsIncomplete bool `json:"isIncomplete,omitempty"`
Status string `json:"status,omitempty"`
CreatedBy string `json:"createdBy,omitempty"`
UpdatedBy string `json:"updatedBy,omitempty"`
CreateTime int64 `json:"createTime,omitempty"`
UpdateTime int64 `json:"updateTime,omitempty"`
Version int `json:"version,omitempty"`
Terms []struct {
Guid string `json:"guid"`
TypeName string `json:"typeName"`
EntityStatus string `json:"entityStatus"`
DisplayText string `json:"displayText"`
RelationshipType string `json:"relationshipType"`
RelationshipGuid string `json:"relationshipGuid"`
RelationshipStatus string `json:"relationshipStatus"`
RelationshipAttributes struct {
TypeName string `json:"typeName"`
} `json:"relationshipAttributes"`
} `json:"terms,omitempty"`
RelationshipAttributes struct {
SchemaRegistrySubjects []interface{} `json:"schemaRegistrySubjects,omitempty"`
McMonitors []interface{} `json:"mcMonitors,omitempty"`
OutputPortDataProducts []interface{} `json:"outputPortDataProducts,omitempty"`
Files []interface{} `json:"files,omitempty"`
McIncidents []interface{} `json:"mcIncidents,omitempty"`
Links []interface{} `json:"links,omitempty"`
Categories []interface{} `json:"categories,omitempty"`
Metrics []interface{} `json:"metrics,omitempty"`
Readme interface{} `json:"readme,omitempty"`
Meanings []interface{} `json:"meanings,omitempty"`
SodaChecks []interface{} `json:"sodaChecks,omitempty"`
} `json:"relationshipAttributes,omitempty"`
Labels []interface{} `json:"labels,omitempty"`
}

// GlossaryAttributes represents the attributes of a glossary in Atlan.
type GlossaryAttributes struct {
PopularityScore float64 `json:"popularityScore,omitempty"`
AssetDbtJobLastRunQueuedDuration interface{} `json:"assetDbtJobLastRunQueuedDuration,omitempty"`
AssetMcMonitorNames []string `json:"assetMcMonitorNames,omitempty"`
Usage interface{} `json:"usage,omitempty"`
HasLineage bool `json:"__hasLineage,omitempty"`
AssetDbtTestStatus interface{} `json:"assetDbtTestStatus,omitempty"`
LastSyncRun interface{} `json:"lastSyncRun,omitempty"`
AssetSodaLastSyncRunAt int `json:"assetSodaLastSyncRunAt,omitempty"`
StarredCount int `json:"starredCount,omitempty"`
LastRowChangedAt int `json:"lastRowChangedAt,omitempty"`
SourceReadRecentUserList []interface{} `json:"sourceReadRecentUserList,omitempty"`
AssetMcIncidentQualifiedNames []interface{} `json:"assetMcIncidentQualifiedNames,omitempty"`
AssetMcIncidentTypes []interface{} `json:"assetMcIncidentTypes,omitempty"`
AssetSodaLastScanAt int `json:"assetSodaLastScanAt,omitempty"`
SourceUpdatedAt int `json:"sourceUpdatedAt,omitempty"`
AssetDbtJobLastRunArtifactsSaved bool `json:"assetDbtJobLastRunArtifactsSaved,omitempty"`
StarredDetailsList []interface{} `json:"starredDetailsList,omitempty"`
AssetDbtJobLastRunQueuedDurationHumanized interface{} `json:"assetDbtJobLastRunQueuedDurationHumanized,omitempty"`
AssetDbtJobStatus interface{} `json:"assetDbtJobStatus,omitempty"`
AssetDbtJobLastRunArtifactS3Path interface{} `json:"assetDbtJobLastRunArtifactS3Path,omitempty"`
CertificateStatusMessage interface{} `json:"certificateStatusMessage,omitempty"`
SourceCreatedAt int `json:"sourceCreatedAt,omitempty"`
AssetDbtJobLastRunDequedAt int `json:"assetDbtJobLastRunDequedAt,omitempty"`
AssetDbtTags []interface{} `json:"assetDbtTags,omitempty"`
SourceReadSlowQueryRecordList []interface{} `json:"sourceReadSlowQueryRecordList,omitempty"`
AssetDbtAccountName interface{} `json:"assetDbtAccountName,omitempty"`
SourceQueryComputeCostList []interface{} `json:"sourceQueryComputeCostList,omitempty"`
AssetDbtJobLastRunOwnerThreadId interface{} `json:"assetDbtJobLastRunOwnerThreadId,omitempty"`
AssetDbtJobLastRunNotificationsSent bool `json:"assetDbtJobLastRunNotificationsSent,omitempty"`
AssetDbtEnvironmentDbtVersion interface{} `json:"assetDbtEnvironmentDbtVersion,omitempty"`
AssetDbtMeta interface{} `json:"assetDbtMeta,omitempty"`
AssetMcMonitorTypes []interface{} `json:"assetMcMonitorTypes,omitempty"`
GlossaryType interface{} `json:"glossaryType,omitempty"`
AssetDbtJobLastRunTotalDuration interface{} `json:"assetDbtJobLastRunTotalDuration,omitempty"`
AssetSodaCheckCount int `json:"assetSodaCheckCount,omitempty"`
Examples []interface{} `json:"examples,omitempty"`
SourceLastReadAt int `json:"sourceLastReadAt,omitempty"`
AssetDbtJobLastRunTotalDurationHumanized interface{} `json:"assetDbtJobLastRunTotalDurationHumanized,omitempty"`
SubType interface{} `json:"subType,omitempty"`
AssetMcIncidentSeverities []interface{} `json:"assetMcIncidentSeverities,omitempty"`
ConnectionName interface{} `json:"connectionName,omitempty"`
AssetDbtSourceFreshnessCriteria interface{} `json:"assetDbtSourceFreshnessCriteria,omitempty"`
Metrics []interface{} `json:"metrics,omitempty"`
AdditionalAttributes interface{} `json:"additionalAttributes,omitempty"`
AssetSodaCheckStatuses interface{} `json:"assetSodaCheckStatuses,omitempty"`
CertificateStatus string `json:"certificateStatus,omitempty"`
AssetDbtJobLastRunExecutedByThreadId interface{} `json:"assetDbtJobLastRunExecutedByThreadId,omitempty"`
ReplicatedFrom interface{} `json:"replicatedFrom,omitempty"`
AssetDbtJobLastRunHasSourcesGenerated bool `json:"assetDbtJobLastRunHasSourcesGenerated,omitempty"`
DisplayName interface{} `json:"displayName,omitempty"`
SourceCostUnit interface{} `json:"sourceCostUnit,omitempty"`
AssetDbtUniqueId interface{} `json:"assetDbtUniqueId,omitempty"`
AssetSodaDQStatus interface{} `json:"assetSodaDQStatus,omitempty"`
TermType interface{} `json:"termType,omitempty"`
AssetDbtJobLastRunHasDocsGenerated bool `json:"assetDbtJobLastRunHasDocsGenerated,omitempty"`
AssetTags []interface{} `json:"assetTags,omitempty"`
AssetDbtSemanticLayerProxyUrl interface{} `json:"assetDbtSemanticLayerProxyUrl,omitempty"`
CertificateUpdatedBy string `json:"certificateUpdatedBy,omitempty"`
AssetMcMonitorQualifiedNames []interface{} `json:"assetMcMonitorQualifiedNames,omitempty"`
AssetDbtJobLastRunStartedAt int `json:"assetDbtJobLastRunStartedAt,omitempty"`
AnnouncementType interface{} `json:"announcementType,omitempty"`
ViewerUsers []interface{} `json:"viewerUsers,omitempty"`
ViewScore float64 `json:"viewScore,omitempty"`
SourceOwners interface{} `json:"sourceOwners,omitempty"`
UserDescription string `json:"userDescription,omitempty"`
AdminGroups []interface{} `json:"adminGroups,omitempty"`
AssetSodaSourceURL interface{} `json:"assetSodaSourceURL,omitempty"`
AssetDbtJobLastRunCreatedAt int `json:"assetDbtJobLastRunCreatedAt,omitempty"`
AssetDbtJobNextRun int `json:"assetDbtJobNextRun,omitempty"`
AssetCoverImage interface{} `json:"assetCoverImage,omitempty"`
Abbreviation interface{} `json:"abbreviation,omitempty"`
SourceReadPopularQueryRecordList []interface{} `json:"sourceReadPopularQueryRecordList,omitempty"`
SourceTotalCost float64 `json:"sourceTotalCost,omitempty"`
TenantId interface{} `json:"tenantId,omitempty"`
AnnouncementMessage interface{} `json:"announcementMessage,omitempty"`
SourceEmbedURL interface{} `json:"sourceEmbedURL,omitempty"`
AssetDbtJobLastRunUrl interface{} `json:"assetDbtJobLastRunUrl,omitempty"`
Name string `json:"name,omitempty"`
QualifiedName string `json:"qualifiedName,omitempty"`
AssetIcon string `json:"assetIcon,omitempty"`
}

// GlossaryTerm represents a term in a glossary in Atlan.
type GlossaryTerm struct {
TypeName string `json:"typeName"`
Attributes GlossaryAttributes `json:"attributes"`
Guid string `json:"guid"`
IsIncomplete bool `json:"isIncomplete"`
Status string `json:"status"`
CreatedBy string `json:"createdBy"`
UpdatedBy string `json:"updatedBy"`
CreateTime int64 `json:"createTime"`
UpdateTime int64 `json:"updateTime"`
Version int `json:"version"`
ValidValuesFor []interface{} `json:"validValuesFor"`
SchemaRegistrySubjects []interface{} `json:"schemaRegistrySubjects"`
ValidValues []interface{} `json:"validValues"`
SeeAlso []interface{} `json:"seeAlso"`
IsA []interface{} `json:"isA"`
Antonyms []interface{} `json:"antonyms"`
AssignedEntities []interface{} `json:"assignedEntities"`
McIncidents []interface{} `json:"mcIncidents"`
Links []interface{} `json:"links"`
Classifies []interface{} `json:"classifies"`
Categories []interface{} `json:"categories"`
PreferredToTerms []interface{} `json:"preferredToTerms"`
PreferredTerms []interface{} `json:"preferredTerms"`
TranslationTerms []interface{} `json:"translationTerms"`
Synonyms []interface{} `json:"synonyms"`
ReplacedBy []interface{} `json:"replacedBy"`
OutputPortDataProducts []interface{} `json:"outputPortDataProducts"`
Readme interface{} `json:"readme"`
ReplacementTerms []interface{} `json:"replacementTerms"`
Meanings []interface{} `json:"meanings"`
McMonitors []interface{} `json:"mcMonitors"`
TranslatedTerms []interface{} `json:"translatedTerms"`
Files []interface{} `json:"files"`
Metrics []interface{} `json:"metrics"`
SodaChecks []interface{} `json:"sodaChecks"`
Tags []AtlanTag `json:"classifications"`
Anchor struct {
Guid string `json:"guid"`
TypeName string `json:"typeName"`
EntityStatus string `json:"entityStatus"`
DisplayText string `json:"displayText"`
RelationshipType string `json:"relationshipType"`
RelationshipGuid string `json:"relationshipGuid"`
RelationshipStatus string `json:"relationshipStatus"`
RelationshipAttributes struct {
TypeName string `json:"typeName"`
} `json:"relationshipAttributes"`
} `json:"anchor"`
Labels []interface{} `json:"labels"`
func (ag *AtlasGlossary) FromJSON(data []byte) error {
return json.Unmarshal(data, ag)
}
11 changes: 4 additions & 7 deletions atlan/model/assets/glossary_term.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,10 @@ func (gt *AtlasGlossaryTerm) UnmarshalJSON(data []byte) error {
return nil
}

func FromJSONTerm(data []byte) (*AtlasGlossaryTerm, error) {
var glossaryResponse AtlasGlossaryTerm
err := json.Unmarshal(data, &glossaryResponse)

return &glossaryResponse, err
func (ag *AtlasGlossaryTerm) FromJSON(data []byte) error {
return json.Unmarshal(data, ag)
}

func ToJSONTerm(glossary *AtlasGlossaryTerm) ([]byte, error) {
return json.Marshal(glossary)
func (gt *AtlasGlossaryTerm) ToJSON() ([]byte, error) {
return json.Marshal(gt)
}
10 changes: 7 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package main

import (
"fmt"
"github.com/atlanhq/atlan-go/atlan"
"github.com/atlanhq/atlan-go/atlan/client"
"github.com/atlanhq/atlan-go/atlan/model/assets"
)

func main() {
Expand All @@ -13,8 +13,12 @@ func main() {
//ctx, _ := client.Context("API_KEY", "BASE_URL")
ctx.SetLogger(true, "debug")

response, _ := client.Get(atlan.AtlanTypeCategoryClassification)
fmt.Println(response.AtlanTagDefs[0].GUID)
glossary, err := client.GetByGuid[*assets.AtlasGlossaryTerm]("a034e099-bbba-4c1d-84d8-ca3f3a406124")
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Response:", *glossary.TypeName)
}

/*
client.GetAll()
Expand Down

0 comments on commit a993489

Please sign in to comment.