github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/mccp/mccpv2/shared_domain.go (about)

     1  package mccpv2
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/IBM-Cloud/bluemix-go/bmxerror"
     7  	"github.com/IBM-Cloud/bluemix-go/client"
     8  	"github.com/IBM-Cloud/bluemix-go/rest"
     9  )
    10  
    11  //ErrCodeSharedDomainDoesnotExist ...
    12  var ErrCodeSharedDomainDoesnotExist = "SharedDomainDoesnotExist"
    13  
    14  //SharedDomainRequest ...
    15  type SharedDomainRequest struct {
    16  	Name            string `json:"name"`
    17  	RouterGroupGUID string `json:"router_group_guid,omitempty"`
    18  }
    19  
    20  //SharedDomaineMetadata ...
    21  type SharedDomainMetadata struct {
    22  	GUID string `json:"guid"`
    23  	URL  string `json:"url"`
    24  }
    25  
    26  //SharedDomainEntity ...
    27  type SharedDomainEntity struct {
    28  	Name            string `json:"name"`
    29  	RouterGroupGUID string `json:"router_group_guid"`
    30  	RouterGroupType string `json:"router_group_type"`
    31  }
    32  
    33  //SharedDomainResource ...
    34  type SharedDomainResource struct {
    35  	Resource
    36  	Entity SharedDomainEntity
    37  }
    38  
    39  //SharedDomainFields ...
    40  type SharedDomainFields struct {
    41  	Metadata SharedDomainMetadata
    42  	Entity   SharedDomainEntity
    43  }
    44  
    45  //ToFields ..
    46  func (resource SharedDomainResource) ToFields() SharedDomain {
    47  	entity := resource.Entity
    48  
    49  	return SharedDomain{
    50  		GUID:            resource.Metadata.GUID,
    51  		Name:            entity.Name,
    52  		RouterGroupGUID: entity.RouterGroupGUID,
    53  		RouterGroupType: entity.RouterGroupType,
    54  	}
    55  }
    56  
    57  //SharedDomain model
    58  type SharedDomain struct {
    59  	GUID            string
    60  	Name            string
    61  	RouterGroupGUID string
    62  	RouterGroupType string
    63  }
    64  
    65  //SharedDomains ...
    66  type SharedDomains interface {
    67  	FindByName(domainName string) (*SharedDomain, error)
    68  	Create(req SharedDomainRequest, opts ...bool) (*SharedDomainFields, error)
    69  	Get(sharedDomainGUID string) (*SharedDomainFields, error)
    70  	Delete(sharedDomainGUID string, opts ...bool) error
    71  }
    72  
    73  type sharedDomain struct {
    74  	client *client.Client
    75  }
    76  
    77  func newSharedDomainAPI(c *client.Client) SharedDomains {
    78  	return &sharedDomain{
    79  		client: c,
    80  	}
    81  }
    82  
    83  func (d *sharedDomain) FindByName(domainName string) (*SharedDomain, error) {
    84  	rawURL := "/v2/shared_domains"
    85  	req := rest.GetRequest(rawURL).Query("q", "name:"+domainName)
    86  	httpReq, err := req.Build()
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  	path := httpReq.URL.String()
    91  	domain, err := listSharedDomainWithPath(d.client, path)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  	if len(domain) == 0 {
    96  		return nil, bmxerror.New(ErrCodeSharedDomainDoesnotExist, fmt.Sprintf("Shared Domain: %q doesn't exist", domainName))
    97  	}
    98  	return &domain[0], nil
    99  }
   100  
   101  func listSharedDomainWithPath(c *client.Client, path string) ([]SharedDomain, error) {
   102  	var sharedDomain []SharedDomain
   103  	_, err := c.GetPaginated(path, NewCCPaginatedResources(SharedDomainResource{}), func(resource interface{}) bool {
   104  		if sharedDomainResource, ok := resource.(SharedDomainResource); ok {
   105  			sharedDomain = append(sharedDomain, sharedDomainResource.ToFields())
   106  			return true
   107  		}
   108  		return false
   109  	})
   110  	return sharedDomain, err
   111  }
   112  
   113  // opts is list of boolean parametes
   114  // opts[0] - async - Will run the create request in a background job. Recommended: 'true'. Default to 'true'
   115  
   116  func (d *sharedDomain) Create(req SharedDomainRequest, opts ...bool) (*SharedDomainFields, error) {
   117  	async := true
   118  	if len(opts) > 0 {
   119  		async = opts[0]
   120  	}
   121  	rawURL := fmt.Sprintf("/v2/shared_domains?async=%t", async)
   122  	sharedDomainFields := SharedDomainFields{}
   123  	_, err := d.client.Post(rawURL, req, &sharedDomainFields)
   124  	if err != nil {
   125  		return nil, err
   126  	}
   127  	return &sharedDomainFields, nil
   128  }
   129  
   130  func (d *sharedDomain) Get(sharedDomainGUID string) (*SharedDomainFields, error) {
   131  	rawURL := fmt.Sprintf("/v2/shared_domains/%s", sharedDomainGUID)
   132  	sharedDomainFields := SharedDomainFields{}
   133  	_, err := d.client.Get(rawURL, &sharedDomainFields, nil)
   134  	if err != nil {
   135  		return nil, err
   136  	}
   137  	return &sharedDomainFields, nil
   138  }
   139  
   140  // opts is list of boolean parametes
   141  // opts[0] - async - Will run the delete request in a background job. Recommended: 'true'. Default to 'true'
   142  
   143  func (d *sharedDomain) Delete(sharedDomainGUID string, opts ...bool) error {
   144  	async := true
   145  	if len(opts) > 0 {
   146  		async = opts[0]
   147  	}
   148  	rawURL := fmt.Sprintf("/v2/shared_domains/%s?async=%t", sharedDomainGUID, async)
   149  	_, err := d.client.Delete(rawURL)
   150  	return err
   151  }