sigs.k8s.io/cluster-api-provider-aws@v1.5.5/pkg/cloud/scope/global.go (about)

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     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 scope
    18  
    19  import (
    20  	awsclient "github.com/aws/aws-sdk-go/aws/client"
    21  	"github.com/pkg/errors"
    22  
    23  	"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/throttle"
    24  )
    25  
    26  // NewGlobalScope creates a new Scope from the supplied parameters.
    27  func NewGlobalScope(params GlobalScopeParams) (*GlobalScope, error) {
    28  	if params.Region == "" {
    29  		return nil, errors.New("region required to create session")
    30  	}
    31  	if params.ControllerName == "" {
    32  		return nil, errors.New("controller name required to generate global scope")
    33  	}
    34  	ns, limiters, err := sessionForRegion(params.Region, params.Endpoints)
    35  	if err != nil {
    36  		return nil, errors.Wrap(err, "failed to create aws session")
    37  	}
    38  	return &GlobalScope{
    39  		session:         ns,
    40  		serviceLimiters: limiters,
    41  		controllerName:  params.ControllerName,
    42  	}, nil
    43  }
    44  
    45  // GlobalScopeParams defines the parameters acceptable for GlobalScope.
    46  type GlobalScopeParams struct {
    47  	ControllerName string
    48  	Region         string
    49  	Endpoints      []ServiceEndpoint
    50  }
    51  
    52  // GlobalScope defines the specs for the GlobalScope.
    53  type GlobalScope struct {
    54  	session         awsclient.ConfigProvider
    55  	serviceLimiters throttle.ServiceLimiters
    56  	controllerName  string
    57  }
    58  
    59  // Session returns the AWS SDK session. Used for creating clients.
    60  func (s *GlobalScope) Session() awsclient.ConfigProvider {
    61  	return s.session
    62  }
    63  
    64  // ServiceLimiter returns the AWS SDK session. Used for creating clients.
    65  func (s *GlobalScope) ServiceLimiter(service string) *throttle.ServiceLimiter {
    66  	if sl, ok := s.serviceLimiters[service]; ok {
    67  		return sl
    68  	}
    69  	return nil
    70  }
    71  
    72  // ControllerName returns the name of the controller that
    73  // created the GlobalScope.
    74  func (s *GlobalScope) ControllerName() string {
    75  	return s.controllerName
    76  }