github.com/Axway/agent-sdk@v1.1.101/pkg/harvester/harvesterevent.go (about) 1 package harvester 2 3 import ( 4 "strings" 5 6 "github.com/Axway/agent-sdk/pkg/watchmanager/proto" 7 ) 8 9 type harvesterResourceReference struct { 10 ID string `json:"id,omitempty"` 11 Kind string `json:"kind,omitempty"` 12 Name string `json:"name,omitempty"` 13 ScopeKind string `json:"scopeKind,omitempty"` 14 ScopeName string `json:"scopeName,omitempty"` 15 SelfLink string `json:"selfLink,omitempty"` 16 Type string `json:"type,omitempty"` 17 } 18 19 // HarvesterResourceMetadata that all server resources have. Data is generated by the server. 20 type harvesterResourceMetadata struct { 21 ID string `json:"id,omitempty"` 22 Scope *proto.Metadata_ScopeKind `json:"scope,omitempty"` 23 SelfLink string `json:"selfLink,omitempty"` 24 References []*harvesterResourceReference `json:"references,omitempty"` 25 } 26 27 type harvesterResourceInstance struct { 28 Group string `json:"group,omitempty"` 29 Kind string `json:"kind,omitempty"` 30 Name string `json:"name,omitempty"` 31 Metadata *harvesterResourceMetadata `json:"metadata,omitempty"` 32 Attributes map[string]string `json:"attributes,omitempty"` 33 } 34 35 type resourceEntryExternalEvent struct { 36 ID string `json:"id,omitempty"` 37 Time string `json:"time,omitempty"` 38 Version string `json:"version,omitempty"` 39 Product string `json:"product,omitempty"` 40 CorrelationID string `json:"correlationId,omitempty"` 41 Organization *proto.Organization `json:"organization,omitempty"` 42 Type string `json:"type,omitempty"` 43 Payload *harvesterResourceInstance `json:"payload,omitempty"` 44 Metadata *proto.EventMeta `json:"metadata,omitempty"` 45 } 46 47 func (r *resourceEntryExternalEvent) toProtoEvent() *proto.Event { 48 return &proto.Event{ 49 Id: r.ID, 50 Time: r.Time, 51 Version: r.Version, 52 Product: r.Product, 53 CorrelationId: r.CorrelationID, 54 Organization: r.Organization, 55 Type: r.convertProtoType(), 56 Payload: r.convertPayload(), 57 Metadata: r.convertEventMetadata(), 58 } 59 } 60 61 func (r *resourceEntryExternalEvent) convertEventMetadata() *proto.EventMeta { 62 if r.Metadata != nil { 63 return &proto.EventMeta{ 64 WatchTopicID: r.Metadata.WatchTopicID, 65 WatchTopicSelfLink: r.Metadata.WatchTopicSelfLink, 66 SequenceID: r.Metadata.SequenceID, 67 Subresource: r.Metadata.Subresource, 68 } 69 } 70 return nil 71 } 72 73 func (r *resourceEntryExternalEvent) convertProtoType() proto.Event_Type { 74 t := strings.ToUpper(strings.TrimPrefix(r.Type, "Resource")) 75 eventType, ok := proto.Event_Type_value[t] 76 if ok { 77 return proto.Event_Type(eventType) 78 } 79 return proto.Event_CREATED 80 } 81 82 func (r *resourceEntryExternalEvent) convertPayload() *proto.ResourceInstance { 83 if r.Payload != nil { 84 return &proto.ResourceInstance{ 85 Group: r.Payload.Group, 86 Kind: r.Payload.Kind, 87 Name: r.Payload.Name, 88 Metadata: &proto.Metadata{ 89 Id: r.Payload.Metadata.ID, 90 Scope: r.Payload.Metadata.Scope, 91 SelfLink: r.Payload.Metadata.SelfLink, 92 References: r.convertResourceReferences(), 93 }, 94 Attributes: r.Payload.Attributes, 95 } 96 } 97 return nil 98 } 99 100 func (r *resourceEntryExternalEvent) convertResourceReferences() []*proto.Reference { 101 if r.Payload.Metadata.References != nil { 102 references := make([]*proto.Reference, 0) 103 for _, hRef := range r.Payload.Metadata.References { 104 ref := &proto.Reference{ 105 Id: hRef.ID, 106 Kind: hRef.Kind, 107 Name: hRef.Name, 108 ScopeKind: hRef.ScopeKind, 109 ScopeName: hRef.ScopeName, 110 SelfLink: hRef.SelfLink, 111 Type: r.convertReferenceType(hRef.Type), 112 } 113 references = append(references, ref) 114 } 115 return references 116 } 117 return nil 118 } 119 120 func (r *resourceEntryExternalEvent) convertReferenceType(strReferenceType string) proto.Reference_Type { 121 referenceType, ok := proto.Reference_Type_value[strReferenceType] 122 if ok { 123 return proto.Reference_Type(referenceType) 124 } 125 return proto.Reference_HARD 126 }