github.com/argoproj/argo-events@v1.9.1/sensors/artifacts/resource.go (about)

     1  /*
     2  Copyright 2018 BlackRock, Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package artifacts
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  
    23  	"github.com/ghodss/yaml"
    24  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    25  
    26  	"github.com/argoproj/argo-events/common/logging"
    27  	"github.com/argoproj/argo-events/pkg/apis/common"
    28  )
    29  
    30  // ResourceReader implements the ArtifactReader interface for resource artifacts
    31  type ResourceReader struct {
    32  	resourceArtifact *unstructured.Unstructured
    33  }
    34  
    35  // NewResourceReader creates a new ArtifactReader for resource
    36  func NewResourceReader(resourceArtifact *common.Resource) (ArtifactReader, error) {
    37  	if resourceArtifact == nil {
    38  		return nil, fmt.Errorf("ResourceArtifact does not exist")
    39  	}
    40  	data, err := json.Marshal(resourceArtifact)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	object := make(map[string]interface{})
    45  	err = json.Unmarshal(data, &object)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	un := &unstructured.Unstructured{Object: object}
    50  	return &ResourceReader{un}, nil
    51  }
    52  
    53  func (reader *ResourceReader) Read() ([]byte, error) {
    54  	log := logging.NewArgoEventsLogger()
    55  	log.Debugw("reading artifact from resource template", "resource", reader.resourceArtifact.Object)
    56  	return yaml.Marshal(reader.resourceArtifact.Object)
    57  }