github.com/ystia/yorc/v4@v4.3.0/plugin/infra_usage_collector.go (about)

     1  // Copyright 2018 Bull S.A.S. Atos Technologies - Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois, France.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package plugin
    16  
    17  import (
    18  	"context"
    19  	"net/rpc"
    20  
    21  	plugin "github.com/hashicorp/go-plugin"
    22  	"github.com/pkg/errors"
    23  
    24  	"github.com/ystia/yorc/v4/config"
    25  	"github.com/ystia/yorc/v4/events"
    26  	"github.com/ystia/yorc/v4/prov"
    27  )
    28  
    29  // InfraUsageCollector is an extension of prov.InfraStructureUsageCollector
    30  type InfraUsageCollector interface {
    31  	prov.InfraUsageCollector
    32  	GetSupportedInfras() ([]string, error)
    33  }
    34  
    35  // InfraUsageCollectorPlugin is public for use by reflexion and should be considered as private to this package.
    36  // Please do not use it directly.
    37  type InfraUsageCollectorPlugin struct {
    38  	F               func() prov.InfraUsageCollector
    39  	SupportedInfras []string
    40  }
    41  
    42  // Server is public for use by reflexion and should be considered as private to this package.
    43  // Please do not use it directly.
    44  func (p *InfraUsageCollectorPlugin) Server(b *plugin.MuxBroker) (interface{}, error) {
    45  	des := &InfraUsageCollectorServer{Broker: b, SupportedInfras: p.SupportedInfras}
    46  	if p.F != nil {
    47  		des.InfraUsageCollector = p.F()
    48  	}
    49  	return des, nil
    50  }
    51  
    52  // Client is public for use by reflexion and should be considered as private to this package.
    53  // Please do not use it directly.
    54  func (p *InfraUsageCollectorPlugin) Client(b *plugin.MuxBroker, c *rpc.Client) (interface{}, error) {
    55  	return &InfraUsageCollectorClient{Broker: b, Client: c}, nil
    56  }
    57  
    58  // InfraUsageCollectorClient is public for use by reflexion and should be considered as private to this package.
    59  // Please do not use it directly.
    60  type InfraUsageCollectorClient struct {
    61  	Broker *plugin.MuxBroker
    62  	Client *rpc.Client
    63  }
    64  
    65  // GetUsageInfo is public for use by reflexion and should be considered as private to this package.
    66  // Please do not use it directly.
    67  func (c *InfraUsageCollectorClient) GetUsageInfo(ctx context.Context, cfg config.Configuration, taskID, infraName, locationName string,
    68  	params map[string]string) (map[string]interface{}, error) {
    69  	lof, ok := events.FromContext(ctx)
    70  	if !ok {
    71  		return nil, errors.New("Missing contextual log optionnal fields")
    72  	}
    73  
    74  	id := c.Broker.NextId()
    75  	closeChan := make(chan struct{}, 0)
    76  	defer close(closeChan)
    77  	go clientMonitorContextCancellation(ctx, closeChan, id, c.Broker)
    78  
    79  	var resp InfraUsageCollectorGetUsageInfoResponse
    80  	args := &InfraUsageCollectorGetUsageInfoArgs{
    81  		ChannelID:         id,
    82  		Conf:              cfg,
    83  		TaskID:            taskID,
    84  		InfraName:         infraName,
    85  		LocationName:      locationName,
    86  		Parameters:        params,
    87  		LogOptionalFields: lof,
    88  	}
    89  	err := c.Client.Call("Plugin.GetUsageInfo", args, &resp)
    90  	if err != nil {
    91  		return nil, errors.Wrap(err, "Failed to get usage info for infra collector plugin")
    92  	}
    93  	return resp.UsageInfo, toError(resp.Error)
    94  }
    95  
    96  // GetSupportedInfras is public for use by reflexion and should be considered as private to this package.
    97  // Please do not use it directly.
    98  func (c *InfraUsageCollectorClient) GetSupportedInfras() ([]string, error) {
    99  	var resp InfraUsageCollectorGetSupportedInfrasResponse
   100  	err := c.Client.Call("Plugin.GetSupportedInfras", new(interface{}), &resp)
   101  	if err != nil {
   102  		return nil, errors.Wrap(err, "Failed to get supported infra for infra collector plugin")
   103  	}
   104  
   105  	return resp.Infras, toError(resp.Error)
   106  }
   107  
   108  // InfraUsageCollectorServer is public for use by reflexion and should be considered as private to this package.
   109  // Please do not use it directly.
   110  type InfraUsageCollectorServer struct {
   111  	Broker              *plugin.MuxBroker
   112  	InfraUsageCollector prov.InfraUsageCollector
   113  	SupportedInfras     []string
   114  }
   115  
   116  // InfraUsageCollectorGetUsageInfoArgs is public for use by reflexion and should be considered as private to this package.
   117  // Please do not use it directly.
   118  type InfraUsageCollectorGetUsageInfoArgs struct {
   119  	ChannelID         uint32
   120  	Conf              config.Configuration
   121  	TaskID            string
   122  	InfraName         string
   123  	LocationName      string
   124  	Parameters        map[string]string
   125  	LogOptionalFields events.LogOptionalFields
   126  }
   127  
   128  // InfraUsageCollectorGetUsageInfoResponse is public for use by reflexion and should be considered as private to this package.
   129  // Please do not use it directly.
   130  type InfraUsageCollectorGetUsageInfoResponse struct {
   131  	UsageInfo map[string]interface{}
   132  	Error     *RPCError
   133  }
   134  
   135  // InfraUsageCollectorGetSupportedInfrasResponse is public for use by reflexion and should be considered as private to this package.
   136  // Please do not use it directly.
   137  type InfraUsageCollectorGetSupportedInfrasResponse struct {
   138  	Infras []string
   139  	Error  *RPCError
   140  }
   141  
   142  // GetUsageInfo is public for use by reflexion and should be considered as private to this package.
   143  // Please do not use it directly.
   144  func (s *InfraUsageCollectorServer) GetUsageInfo(args *InfraUsageCollectorGetUsageInfoArgs, reply *InfraUsageCollectorGetUsageInfoResponse) error {
   145  	ctx, cancelFunc := context.WithCancel(events.NewContext(context.Background(), args.LogOptionalFields))
   146  	defer cancelFunc()
   147  
   148  	go s.Broker.AcceptAndServe(args.ChannelID, &RPCContextCanceller{CancelFunc: cancelFunc})
   149  	usageInfo, err := s.InfraUsageCollector.GetUsageInfo(ctx, args.Conf, args.TaskID,
   150  		args.InfraName, args.LocationName, args.Parameters)
   151  
   152  	var resp InfraUsageCollectorGetUsageInfoResponse
   153  	resp.UsageInfo = usageInfo
   154  
   155  	if err != nil {
   156  		resp.Error = NewRPCError(err)
   157  	}
   158  	*reply = resp
   159  	return nil
   160  }
   161  
   162  // GetSupportedInfras is public for use by reflexion and should be considered as private to this package.
   163  // Please do not use it directly.
   164  func (s *InfraUsageCollectorServer) GetSupportedInfras(_ interface{}, reply *InfraUsageCollectorGetSupportedInfrasResponse) error {
   165  	*reply = InfraUsageCollectorGetSupportedInfrasResponse{Infras: s.SupportedInfras}
   166  	return nil
   167  }