github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/cdn/v1/domains/requests.go (about)

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