github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/cdn/v1/domains/requests.go (about)

     1  package domains
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  )
     6  
     7  // ExtensionOpts allows extensions to add parameters to some requests
     8  // the possible requests include get,delete,enable or disable.
     9  type ExtensionOpts struct {
    10  	// specifies the enterprise_project_id.
    11  	EnterpriseProjectId string `q:"enterprise_project_id"`
    12  }
    13  
    14  // ToExtensionQuery formats a ExtensionOpts into a query string.
    15  func (opts ExtensionOpts) ToExtensionQuery() (string, error) {
    16  	q, err := golangsdk.BuildQueryString(opts)
    17  	return q.String(), err
    18  }
    19  
    20  // SourcesOpts specifies the domain name or the IP address of the origin server
    21  type SourcesOpts struct {
    22  	IporDomain    string `json:"ip_or_domain" required:"true"`
    23  	OriginType    string `json:"origin_type" required:"true"`
    24  	ActiveStandby int    `json:"active_standby" required:"true"`
    25  }
    26  
    27  // CreateOpts specifies the attributes used to create a CDN domain.
    28  type CreateOpts struct {
    29  	// the acceleration domain name, the length of a label is within 50 characters.
    30  	DomainName string `json:"domain_name" required:"true"`
    31  	// the service type, valid values are web, downlaod and video
    32  	BusinessType string `json:"business_type" required:"true"`
    33  	// the domain name or the IP address of the origin server
    34  	Sources []SourcesOpts `json:"sources" required:"true"`
    35  	// the enterprise project ID
    36  	EnterpriseProjectId string `json:"enterprise_project_id,omitempty"`
    37  }
    38  
    39  // CreateOptsBuilder allows extensions to add additional parameters to the
    40  // Create request.
    41  type CreateOptsBuilder interface {
    42  	ToCdnDomainCreateMap() (map[string]interface{}, error)
    43  }
    44  
    45  // ToCdnDomainCreateMap assembles a request body based on the contents of a
    46  // CreateOpts.
    47  func (opts CreateOpts) ToCdnDomainCreateMap() (map[string]interface{}, error) {
    48  	return golangsdk.BuildRequestBody(opts, "domain")
    49  }
    50  
    51  // OriginOpts specifies the attributes used to modify the orogin server.
    52  type OriginOpts struct {
    53  	// the domain name or the IP address of the origin server
    54  	Sources []SourcesOpts `json:"sources" required:"true"`
    55  }
    56  
    57  // OriginOptsBuilder allows extensions to add additional parameters to the
    58  // Origin request.
    59  type OriginOptsBuilder interface {
    60  	ToCdnDomainOriginMap() (map[string]interface{}, error)
    61  }
    62  
    63  // ToCdnDomainOriginMap assembles a request body based on the contents of a
    64  // OriginOpts.
    65  func (opts OriginOpts) ToCdnDomainOriginMap() (map[string]interface{}, error) {
    66  	return golangsdk.BuildRequestBody(opts, "origin")
    67  }
    68  
    69  // Create implements a CDN domain create request.
    70  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    71  	reqBody, err := opts.ToCdnDomainCreateMap()
    72  	if err != nil {
    73  		r.Err = err
    74  		return
    75  	}
    76  
    77  	_, r.Err = client.Post(createURL(client), reqBody, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
    78  	return
    79  }
    80  
    81  // Get retrieves a particular CDN domain based on its unique ID.
    82  func Get(client *golangsdk.ServiceClient, id string, opts *ExtensionOpts) (r GetResult) {
    83  	url := getURL(client, id)
    84  	if opts != nil {
    85  		query, err := opts.ToExtensionQuery()
    86  		if err != nil {
    87  			r.Err = err
    88  			return
    89  		}
    90  		url += query
    91  	}
    92  	_, r.Err = client.Get(url, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
    93  	return
    94  }
    95  
    96  // Delete requests a CDN domain to be deleted to the user in the current tenant.
    97  func Delete(client *golangsdk.ServiceClient, id string, opts *ExtensionOpts) (r DeleteResult) {
    98  	url := deleteURL(client, id)
    99  	if opts != nil {
   100  		query, err := opts.ToExtensionQuery()
   101  		if err != nil {
   102  			r.Err = err
   103  			return
   104  		}
   105  		url += query
   106  	}
   107  
   108  	// Delete requests will response 'domain' body, so we use DeleteWithResponse
   109  	_, r.Err = client.DeleteWithResponse(url, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
   110  	return
   111  }
   112  
   113  // Enable implements a CDN domain enable request.
   114  func Enable(client *golangsdk.ServiceClient, id string, opts *ExtensionOpts) (r EnableResult) {
   115  	url := enableURL(client, id)
   116  	if opts != nil {
   117  		query, err := opts.ToExtensionQuery()
   118  		if err != nil {
   119  			r.Err = err
   120  			return
   121  		}
   122  		url += query
   123  	}
   124  
   125  	_, r.Err = client.Put(url, nil, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
   126  	return
   127  }
   128  
   129  // Disable implements a CDN domain disable request.
   130  func Disable(client *golangsdk.ServiceClient, id string, opts *ExtensionOpts) (r DisableResult) {
   131  	url := disableURL(client, id)
   132  	if opts != nil {
   133  		query, err := opts.ToExtensionQuery()
   134  		if err != nil {
   135  			r.Err = err
   136  			return
   137  		}
   138  		url += query
   139  	}
   140  
   141  	_, r.Err = client.Put(url, nil, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
   142  	return
   143  }
   144  
   145  // Modifying Information About the Origin Server
   146  func Origin(client *golangsdk.ServiceClient, id string, opts *ExtensionOpts, req OriginOptsBuilder) (r OriginResult) {
   147  	url := originURL(client, id)
   148  	if opts != nil {
   149  		// build url with enterprise_project_id
   150  		query, err := opts.ToExtensionQuery()
   151  		if err != nil {
   152  			r.Err = err
   153  			return
   154  		}
   155  		url += query
   156  	}
   157  
   158  	// build request body
   159  	reqBody, err := req.ToCdnDomainOriginMap()
   160  	if err != nil {
   161  		r.Err = err
   162  		return
   163  	}
   164  
   165  	_, r.Err = client.Put(url, reqBody, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
   166  	return
   167  }