github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/satellite/satellitev1/api_service.go (about)

     1  package satellitev1
     2  
     3  import (
     4  	gohttp "net/http"
     5  
     6  	bluemix "github.com/IBM-Cloud/bluemix-go"
     7  	"github.com/IBM-Cloud/bluemix-go/authentication"
     8  	"github.com/IBM-Cloud/bluemix-go/client"
     9  	"github.com/IBM-Cloud/bluemix-go/http"
    10  	"github.com/IBM-Cloud/bluemix-go/rest"
    11  	"github.com/IBM-Cloud/bluemix-go/session"
    12  )
    13  
    14  // ErrCodeAPICreation ...
    15  const ErrCodeAPICreation = "APICreationError"
    16  
    17  // SatelliteServiceAPI is the Aramda K8s client ...
    18  type SatelliteServiceAPI interface {
    19  	Endpoint() Endpoint
    20  	Source() Source
    21  
    22  	//TODO Add other services
    23  }
    24  
    25  type satService struct {
    26  	*client.Client
    27  }
    28  
    29  func New(sess *session.Session) (SatelliteServiceAPI, error) {
    30  	config := sess.Config.Copy()
    31  	err := config.ValidateConfigForService(bluemix.VpcContainerService)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  	if config.HTTPClient == nil {
    36  		config.HTTPClient = http.NewHTTPClient(config)
    37  	}
    38  	tokenRefreher, err := authentication.NewIAMAuthRepository(config, &rest.Client{
    39  		DefaultHeader: gohttp.Header{
    40  			"X-Original-User-Agent": []string{config.UserAgent},
    41  			"User-Agent":            []string{http.UserAgent()},
    42  		},
    43  		HTTPClient: config.HTTPClient,
    44  	})
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	if config.IAMAccessToken == "" {
    49  		err := authentication.PopulateTokens(tokenRefreher, config)
    50  		if err != nil {
    51  			return nil, err
    52  		}
    53  	}
    54  	if config.Endpoint == nil {
    55  		ep, err := config.EndpointLocator.SatelliteEndpoint()
    56  		if err != nil {
    57  			return nil, err
    58  		}
    59  		config.Endpoint = &ep
    60  	}
    61  
    62  	return &satService{
    63  		Client: client.New(config, bluemix.VpcContainerService, tokenRefreher),
    64  	}, nil
    65  }
    66  
    67  // Endpoint implements Endpoint API
    68  func (c *satService) Endpoint() Endpoint {
    69  	return newEndpointAPI(c.Client)
    70  }
    71  
    72  // Source implements Source API
    73  func (c *satService) Source() Source {
    74  	return newSourceAPI(c.Client)
    75  }