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

     1  package management
     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  //ResourceManagementAPI is the resource client ...
    15  type ResourceManagementAPI interface {
    16  	ResourceQuota() ResourceQuotaRepository
    17  	ResourceGroup() ResourceGroupRepository
    18  }
    19  
    20  //ErrCodeAPICreation ...
    21  const ErrCodeAPICreation = "APICreationError"
    22  
    23  //resourceManagementService holds the client
    24  type resourceManagementService struct {
    25  	*client.Client
    26  }
    27  
    28  //New ...
    29  func New(sess *session.Session) (ResourceManagementAPI, error) {
    30  	config := sess.Config.Copy()
    31  	err := config.ValidateConfigForService(bluemix.ResourceManagementService)
    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.ResourceManagementEndpoint()
    56  		if err != nil {
    57  			return nil, err
    58  		}
    59  		config.Endpoint = &ep
    60  	}
    61  	return &resourceManagementService{
    62  		Client: client.New(config, bluemix.ResourceManagementService, tokenRefreher),
    63  	}, nil
    64  }
    65  
    66  //ResourceQuota API
    67  func (a *resourceManagementService) ResourceQuota() ResourceQuotaRepository {
    68  	return newResourceQuotaAPI(a.Client)
    69  }
    70  
    71  //ResourceGroup API
    72  func (a *resourceManagementService) ResourceGroup() ResourceGroupRepository {
    73  	return newResourceGroupAPI(a.Client)
    74  }