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

     1  package cisv1
     2  
     3  import (
     4  	gohttp "net/http"
     5  	"strconv"
     6  
     7  	bluemix "github.com/IBM-Cloud/bluemix-go"
     8  	"github.com/IBM-Cloud/bluemix-go/authentication"
     9  	"github.com/IBM-Cloud/bluemix-go/client"
    10  	"github.com/IBM-Cloud/bluemix-go/http"
    11  	"github.com/IBM-Cloud/bluemix-go/rest"
    12  	"github.com/IBM-Cloud/bluemix-go/session"
    13  )
    14  
    15  //ErrCodeAPICreation ...
    16  const ErrCodeAPICreation = "APICreationError"
    17  
    18  //CisServiceAPI is the Cloud Internet Services API ...
    19  type CisServiceAPI interface {
    20  	Zones() Zones
    21  	Monitors() Monitors
    22  	Pools() Pools
    23  	Glbs() Glbs
    24  	Settings() Settings
    25  	Ips() Ips
    26  	Dns() Dns
    27  	Firewall() Firewall
    28  	RateLimit() RateLimit
    29  }
    30  
    31  //CisService holds the client
    32  type cisService struct {
    33  	*client.Client
    34  }
    35  
    36  //New ...
    37  func New(sess *session.Session) (CisServiceAPI, error) {
    38  	config := sess.Config.Copy()
    39  	err := config.ValidateConfigForService(bluemix.CisService)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	if config.HTTPClient == nil {
    44  		config.HTTPClient = http.NewHTTPClient(config)
    45  	}
    46  	tokenRefreher, err := authentication.NewIAMAuthRepository(config, &rest.Client{
    47  		DefaultHeader: gohttp.Header{
    48  			"X-Original-User-Agent": []string{config.UserAgent},
    49  			"User-Agent":            []string{http.UserAgent()},
    50  		},
    51  		HTTPClient: config.HTTPClient,
    52  	})
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	if config.IAMAccessToken == "" {
    57  		err := authentication.PopulateTokens(tokenRefreher, config)
    58  		if err != nil {
    59  			return nil, err
    60  		}
    61  	}
    62  	if config.Endpoint == nil {
    63  		ep, err := config.EndpointLocator.CisEndpoint()
    64  		if err != nil {
    65  			return nil, err
    66  		}
    67  		config.Endpoint = &ep
    68  	}
    69  
    70  	return &cisService{
    71  		Client: client.New(config, bluemix.CisService, tokenRefreher),
    72  	}, nil
    73  }
    74  
    75  //Zones implement albs API
    76  func (c *cisService) Zones() Zones {
    77  	return newZoneAPI(c.Client)
    78  }
    79  
    80  //Monitors implements Monitors API
    81  func (c *cisService) Monitors() Monitors {
    82  	return newMonitorAPI(c.Client)
    83  }
    84  
    85  //Pools implements Pools API
    86  func (c *cisService) Pools() Pools {
    87  	return newPoolAPI(c.Client)
    88  }
    89  
    90  //Glbs implements Glbs API
    91  func (c *cisService) Glbs() Glbs {
    92  	return newGlbAPI(c.Client)
    93  }
    94  
    95  //Settings implements Settings API
    96  func (c *cisService) Settings() Settings {
    97  	return newSettingsAPI(c.Client)
    98  }
    99  
   100  //Settings implements Settings API
   101  func (c *cisService) Ips() Ips {
   102  	return newIpsAPI(c.Client)
   103  }
   104  
   105  //Settings implements DNS records API
   106  func (c *cisService) Dns() Dns {
   107  	return newDnsAPI(c.Client)
   108  }
   109  
   110  func (c *cisService) Firewall() Firewall {
   111  	return newFirewallAPI(c.Client)
   112  }
   113  
   114  func (c *cisService) RateLimit() RateLimit {
   115  	return newRateLimitAPI(c.Client)
   116  }
   117  
   118  func errorsToString(e []Error) string {
   119  
   120  	var errMsg string
   121  	for _, err := range e {
   122  		errFrag := "Code: " + strconv.Itoa(err.Code) + " " + err.Msg
   123  		errMsg = errMsg + errFrag
   124  	}
   125  	return errMsg
   126  }