github.com/ystia/yorc/v4@v4.3.0/plugin/testdata/plugin-sample/main.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  	"time"
     8  
     9  	"github.com/ystia/yorc/v4/config"
    10  	"github.com/ystia/yorc/v4/events"
    11  	"github.com/ystia/yorc/v4/locations"
    12  	"github.com/ystia/yorc/v4/plugin"
    13  	"github.com/ystia/yorc/v4/prov"
    14  )
    15  
    16  type myDelegateExecutor struct{}
    17  
    18  func (d *myDelegateExecutor) ExecDelegate(ctx context.Context, cfg config.Configuration, taskID, deploymentID, nodeName, delegateOperation string) error {
    19  	log.Printf("Hello from myDelegateExecutor")
    20  
    21  	locationMgr, err := locations.GetManager(cfg)
    22  	if err != nil {
    23  		return err
    24  	}
    25  
    26  	props, err := locationMgr.GetLocationProperties("plugin", "locationType")
    27  	if err != nil {
    28  		for _, k := range props.Keys() {
    29  			log.Printf("configuration key: %s", k)
    30  		}
    31  		log.Printf("Secret key: %q", props.GetStringOrDefault("test", "not found!"))
    32  	}
    33  
    34  	events.SimpleLogEntry(events.LogLevelINFO, deploymentID).RegisterAsString("Hello from myDelegateExecutor")
    35  	return nil
    36  }
    37  
    38  type myOperationExecutor struct{}
    39  
    40  func (d *myOperationExecutor) ExecAsyncOperation(ctx context.Context, conf config.Configuration, taskID, deploymentID, nodeName string, operation prov.Operation, stepName string) (*prov.Action, time.Duration, error) {
    41  	return nil, 0, fmt.Errorf("asynchronous operations %v not yet supported by this sample", operation)
    42  }
    43  
    44  func (d *myOperationExecutor) ExecOperation(ctx context.Context, cfg config.Configuration, taskID, deploymentID, nodeName string, operation prov.Operation) error {
    45  	log.Printf("Hello from myOperationExecutor")
    46  	
    47  	locationMgr, err := locations.GetManager(cfg)
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	props, err := locationMgr.GetLocationProperties("plugin", "locationType")
    53  	if err != nil {
    54  		for _, k := range props.Keys() {
    55  			log.Printf("configuration key: %s", k)
    56  		}
    57  		log.Printf("Secret key: %q", props.GetStringOrDefault("test", "not found!"))
    58  	}
    59  
    60  	events.SimpleLogEntry(events.LogLevelINFO, deploymentID).RegisterAsString("Hello from myOperationExecutor")
    61  	return nil
    62  }
    63  
    64  func main() {
    65  	def := []byte(`tosca_definitions_version: yorc_tosca_simple_yaml_1_0
    66  
    67  metadata:
    68    template_name: yorc-my-types
    69    template_author: Yorc
    70    template_version: 1.0.0
    71  
    72  imports:
    73    - yorc: <yorc-types.yml>
    74  
    75  artifact_types:
    76    yorc.artifacts.Implementation.MyImplementation:
    77      derived_from: tosca.artifacts.Implementation
    78      description: My dummy implementation artifact
    79      file_ext: [ "myext" ]
    80  
    81  node_types:
    82    yorc.my.types.Compute:
    83      derived_from: tosca.nodes.Compute
    84  
    85    yorc.my.types.Soft:
    86      derived_from: tosca.nodes.SoftwareComponent
    87      interfaces:
    88        Standard:
    89          create: dothis.myext
    90  
    91  `)
    92  
    93  	plugin.Serve(&plugin.ServeOpts{
    94  		DelegateFunc: func() prov.DelegateExecutor {
    95  			return new(myDelegateExecutor)
    96  		},
    97  		DelegateSupportedTypes: []string{`yorc\.my\.types\..*`},
    98  		Definitions: map[string][]byte{
    99  			"my-def.yaml": def,
   100  		},
   101  		OperationFunc: func() prov.OperationExecutor {
   102  			return new(myOperationExecutor)
   103  		},
   104  		OperationSupportedArtifactTypes: []string{"yorc.artifacts.Implementation.MyImplementation"},
   105  	})
   106  }