github.com/naphatkrit/deis@v1.12.3/client/controller/models/domains/domains.go (about)

     1  package domains
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"github.com/deis/deis/client/controller/api"
     8  	"github.com/deis/deis/client/controller/client"
     9  )
    10  
    11  // List domains registered with an app.
    12  func List(c *client.Client, appID string, results int) ([]api.Domain, int, error) {
    13  	u := fmt.Sprintf("/v1/apps/%s/domains/", appID)
    14  	body, count, err := c.LimitedRequest(u, results)
    15  
    16  	if err != nil {
    17  		return []api.Domain{}, -1, err
    18  	}
    19  
    20  	var domains []api.Domain
    21  	if err = json.Unmarshal([]byte(body), &domains); err != nil {
    22  		return []api.Domain{}, -1, err
    23  	}
    24  
    25  	return domains, count, nil
    26  }
    27  
    28  // New adds a domain to an app.
    29  func New(c *client.Client, appID string, domain string) (api.Domain, error) {
    30  	u := fmt.Sprintf("/v1/apps/%s/domains/", appID)
    31  
    32  	req := api.DomainCreateRequest{Domain: domain}
    33  
    34  	body, err := json.Marshal(req)
    35  
    36  	if err != nil {
    37  		return api.Domain{}, err
    38  	}
    39  
    40  	resBody, err := c.BasicRequest("POST", u, body)
    41  
    42  	if err != nil {
    43  		return api.Domain{}, err
    44  	}
    45  
    46  	res := api.Domain{}
    47  	if err = json.Unmarshal([]byte(resBody), &res); err != nil {
    48  		return api.Domain{}, err
    49  	}
    50  
    51  	return res, nil
    52  }
    53  
    54  // Delete removes a domain from an app.
    55  func Delete(c *client.Client, appID string, domain string) error {
    56  	u := fmt.Sprintf("/v1/apps/%s/domains/%s", appID, domain)
    57  	_, err := c.BasicRequest("DELETE", u, nil)
    58  	return err
    59  }