github.com/IBM-Cloud/bluemix-go@v0.0.0-20240314082800-4e02a69b84b2/api/iam/iamv1/api_service.go (about)

     1  package iamv1
     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  //IAMServiceAPI is the resource client ...
    15  type IAMServiceAPI interface {
    16  	ServiceRoles() ServiceRoleRepository
    17  	ServiceIds() ServiceIDRepository
    18  	APIKeys() APIKeyRepository
    19  	ServicePolicies() ServicePolicyRepository
    20  	UserPolicies() UserPolicyRepository
    21  	Identity() Identity
    22  }
    23  
    24  //ErrCodeAPICreation ...
    25  const ErrCodeAPICreation = "APICreationError"
    26  
    27  //iamService holds the client
    28  type iamService struct {
    29  	*client.Client
    30  }
    31  
    32  //New ...
    33  func New(sess *session.Session) (IAMServiceAPI, error) {
    34  	config := sess.Config.Copy()
    35  	err := config.ValidateConfigForService(bluemix.IAMService)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	if config.HTTPClient == nil {
    40  		config.HTTPClient = http.NewHTTPClient(config)
    41  	}
    42  	tokenRefreher, err := authentication.NewIAMAuthRepository(config, &rest.Client{
    43  		DefaultHeader: gohttp.Header{
    44  			"X-Original-User-Agent": []string{config.UserAgent},
    45  			"User-Agent":            []string{http.UserAgent()},
    46  		},
    47  		HTTPClient: config.HTTPClient,
    48  	})
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  	if config.IAMAccessToken == "" {
    53  		err := authentication.PopulateTokens(tokenRefreher, config)
    54  		if err != nil {
    55  			return nil, err
    56  		}
    57  	}
    58  	if config.Endpoint == nil {
    59  		ep, err := config.EndpointLocator.IAMEndpoint()
    60  		if err != nil {
    61  			return nil, err
    62  		}
    63  		config.Endpoint = &ep
    64  	}
    65  
    66  	return &iamService{
    67  		Client: client.New(config, bluemix.IAMService, tokenRefreher),
    68  	}, nil
    69  }
    70  
    71  //ServiceRoles API
    72  func (a *iamService) ServiceRoles() ServiceRoleRepository {
    73  	return NewServiceRoleRepository(a.Client)
    74  }
    75  
    76  //ServiceIdsAPI
    77  func (a *iamService) ServiceIds() ServiceIDRepository {
    78  	return NewServiceIDRepository(a.Client)
    79  }
    80  
    81  //APIkeys
    82  func (a *iamService) APIKeys() APIKeyRepository {
    83  	return NewAPIKeyRepository(a.Client)
    84  }
    85  
    86  //ServicePolicyAPI
    87  func (a *iamService) ServicePolicies() ServicePolicyRepository {
    88  	return NewServicePolicyRepository(a.Client)
    89  }
    90  
    91  //UserPoliciesAPI
    92  func (a *iamService) UserPolicies() UserPolicyRepository {
    93  	return NewUserPolicyRepository(a.Client)
    94  }
    95  
    96  //IdentityAPI
    97  func (a *iamService) Identity() Identity {
    98  	return NewIdentity(a.Client)
    99  }