Skip to content

Commit db7d403

Browse files
feat(function): add support for domain (#1040)
Co-authored-by: Rémy Léone <[email protected]>
1 parent 1167f7d commit db7d403

File tree

1 file changed

+286
-0
lines changed

1 file changed

+286
-0
lines changed

api/function/v1beta1/function_sdk.go

Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,46 @@ func (enum *CronStatus) UnmarshalJSON(data []byte) error {
9393
return nil
9494
}
9595

96+
type DomainStatus string
97+
98+
const (
99+
// DomainStatusUnknown is [insert doc].
100+
DomainStatusUnknown = DomainStatus("unknown")
101+
// DomainStatusReady is [insert doc].
102+
DomainStatusReady = DomainStatus("ready")
103+
// DomainStatusDeleting is [insert doc].
104+
DomainStatusDeleting = DomainStatus("deleting")
105+
// DomainStatusError is [insert doc].
106+
DomainStatusError = DomainStatus("error")
107+
// DomainStatusCreating is [insert doc].
108+
DomainStatusCreating = DomainStatus("creating")
109+
// DomainStatusPending is [insert doc].
110+
DomainStatusPending = DomainStatus("pending")
111+
)
112+
113+
func (enum DomainStatus) String() string {
114+
if enum == "" {
115+
// return default value if empty
116+
return "unknown"
117+
}
118+
return string(enum)
119+
}
120+
121+
func (enum DomainStatus) MarshalJSON() ([]byte, error) {
122+
return []byte(fmt.Sprintf(`"%s"`, enum)), nil
123+
}
124+
125+
func (enum *DomainStatus) UnmarshalJSON(data []byte) error {
126+
tmp := ""
127+
128+
if err := json.Unmarshal(data, &tmp); err != nil {
129+
return err
130+
}
131+
132+
*enum = DomainStatus(DomainStatus(tmp).String())
133+
return nil
134+
}
135+
96136
type FunctionPrivacy string
97137

98138
const (
@@ -245,6 +285,42 @@ func (enum *ListCronsRequestOrderBy) UnmarshalJSON(data []byte) error {
245285
return nil
246286
}
247287

288+
type ListDomainsRequestOrderBy string
289+
290+
const (
291+
// ListDomainsRequestOrderByCreatedAtAsc is [insert doc].
292+
ListDomainsRequestOrderByCreatedAtAsc = ListDomainsRequestOrderBy("created_at_asc")
293+
// ListDomainsRequestOrderByCreatedAtDesc is [insert doc].
294+
ListDomainsRequestOrderByCreatedAtDesc = ListDomainsRequestOrderBy("created_at_desc")
295+
// ListDomainsRequestOrderByHostnameAsc is [insert doc].
296+
ListDomainsRequestOrderByHostnameAsc = ListDomainsRequestOrderBy("hostname_asc")
297+
// ListDomainsRequestOrderByHostnameDesc is [insert doc].
298+
ListDomainsRequestOrderByHostnameDesc = ListDomainsRequestOrderBy("hostname_desc")
299+
)
300+
301+
func (enum ListDomainsRequestOrderBy) String() string {
302+
if enum == "" {
303+
// return default value if empty
304+
return "created_at_asc"
305+
}
306+
return string(enum)
307+
}
308+
309+
func (enum ListDomainsRequestOrderBy) MarshalJSON() ([]byte, error) {
310+
return []byte(fmt.Sprintf(`"%s"`, enum)), nil
311+
}
312+
313+
func (enum *ListDomainsRequestOrderBy) UnmarshalJSON(data []byte) error {
314+
tmp := ""
315+
316+
if err := json.Unmarshal(data, &tmp); err != nil {
317+
return err
318+
}
319+
320+
*enum = ListDomainsRequestOrderBy(ListDomainsRequestOrderBy(tmp).String())
321+
return nil
322+
}
323+
248324
type ListFunctionsRequestOrderBy string
249325

250326
const (
@@ -436,6 +512,23 @@ type Cron struct {
436512
Status CronStatus `json:"status"`
437513
}
438514

515+
// Domain: domain
516+
type Domain struct {
517+
ID string `json:"id"`
518+
519+
Hostname string `json:"hostname"`
520+
521+
FunctionID string `json:"function_id"`
522+
523+
URL string `json:"url"`
524+
// Status:
525+
//
526+
// Default value: unknown
527+
Status DomainStatus `json:"status"`
528+
529+
ErrorMessage *string `json:"error_message"`
530+
}
531+
439532
type DownloadURL struct {
440533
URL string `json:"url"`
441534

@@ -490,6 +583,13 @@ type ListCronsResponse struct {
490583
TotalCount uint32 `json:"total_count"`
491584
}
492585

586+
// ListDomainsResponse: list domains response
587+
type ListDomainsResponse struct {
588+
Domains []*Domain `json:"domains"`
589+
590+
TotalCount uint32 `json:"total_count"`
591+
}
592+
493593
// ListFunctionRuntimesResponse: list function runtimes response
494594
type ListFunctionRuntimesResponse struct {
495595
Runtimes []FunctionRuntime `json:"runtimes"`
@@ -1535,6 +1635,173 @@ func (s *API) ListLogs(req *ListLogsRequest, opts ...scw.RequestOption) (*ListLo
15351635
return &resp, nil
15361636
}
15371637

1638+
type ListDomainsRequest struct {
1639+
Region scw.Region `json:"-"`
1640+
1641+
Page *int32 `json:"-"`
1642+
1643+
PageSize *uint32 `json:"-"`
1644+
// OrderBy:
1645+
//
1646+
// Default value: created_at_asc
1647+
OrderBy ListDomainsRequestOrderBy `json:"-"`
1648+
1649+
FunctionID string `json:"-"`
1650+
}
1651+
1652+
func (s *API) ListDomains(req *ListDomainsRequest, opts ...scw.RequestOption) (*ListDomainsResponse, error) {
1653+
var err error
1654+
1655+
if req.Region == "" {
1656+
defaultRegion, _ := s.client.GetDefaultRegion()
1657+
req.Region = defaultRegion
1658+
}
1659+
1660+
defaultPageSize, exist := s.client.GetDefaultPageSize()
1661+
if (req.PageSize == nil || *req.PageSize == 0) && exist {
1662+
req.PageSize = &defaultPageSize
1663+
}
1664+
1665+
query := url.Values{}
1666+
parameter.AddToQuery(query, "page", req.Page)
1667+
parameter.AddToQuery(query, "page_size", req.PageSize)
1668+
parameter.AddToQuery(query, "order_by", req.OrderBy)
1669+
parameter.AddToQuery(query, "function_id", req.FunctionID)
1670+
1671+
if fmt.Sprint(req.Region) == "" {
1672+
return nil, errors.New("field Region cannot be empty in request")
1673+
}
1674+
1675+
scwReq := &scw.ScalewayRequest{
1676+
Method: "GET",
1677+
Path: "/functions/v1beta1/regions/" + fmt.Sprint(req.Region) + "/domains",
1678+
Query: query,
1679+
Headers: http.Header{},
1680+
}
1681+
1682+
var resp ListDomainsResponse
1683+
1684+
err = s.client.Do(scwReq, &resp, opts...)
1685+
if err != nil {
1686+
return nil, err
1687+
}
1688+
return &resp, nil
1689+
}
1690+
1691+
type GetDomainRequest struct {
1692+
Region scw.Region `json:"-"`
1693+
1694+
DomainID string `json:"-"`
1695+
}
1696+
1697+
func (s *API) GetDomain(req *GetDomainRequest, opts ...scw.RequestOption) (*Domain, error) {
1698+
var err error
1699+
1700+
if req.Region == "" {
1701+
defaultRegion, _ := s.client.GetDefaultRegion()
1702+
req.Region = defaultRegion
1703+
}
1704+
1705+
if fmt.Sprint(req.Region) == "" {
1706+
return nil, errors.New("field Region cannot be empty in request")
1707+
}
1708+
1709+
if fmt.Sprint(req.DomainID) == "" {
1710+
return nil, errors.New("field DomainID cannot be empty in request")
1711+
}
1712+
1713+
scwReq := &scw.ScalewayRequest{
1714+
Method: "GET",
1715+
Path: "/functions/v1beta1/regions/" + fmt.Sprint(req.Region) + "/domains/" + fmt.Sprint(req.DomainID) + "",
1716+
Headers: http.Header{},
1717+
}
1718+
1719+
var resp Domain
1720+
1721+
err = s.client.Do(scwReq, &resp, opts...)
1722+
if err != nil {
1723+
return nil, err
1724+
}
1725+
return &resp, nil
1726+
}
1727+
1728+
type CreateDomainRequest struct {
1729+
Region scw.Region `json:"-"`
1730+
1731+
Hostname string `json:"hostname"`
1732+
1733+
FunctionID string `json:"function_id"`
1734+
}
1735+
1736+
func (s *API) CreateDomain(req *CreateDomainRequest, opts ...scw.RequestOption) (*Domain, error) {
1737+
var err error
1738+
1739+
if req.Region == "" {
1740+
defaultRegion, _ := s.client.GetDefaultRegion()
1741+
req.Region = defaultRegion
1742+
}
1743+
1744+
if fmt.Sprint(req.Region) == "" {
1745+
return nil, errors.New("field Region cannot be empty in request")
1746+
}
1747+
1748+
scwReq := &scw.ScalewayRequest{
1749+
Method: "POST",
1750+
Path: "/functions/v1beta1/regions/" + fmt.Sprint(req.Region) + "/domains",
1751+
Headers: http.Header{},
1752+
}
1753+
1754+
err = scwReq.SetBody(req)
1755+
if err != nil {
1756+
return nil, err
1757+
}
1758+
1759+
var resp Domain
1760+
1761+
err = s.client.Do(scwReq, &resp, opts...)
1762+
if err != nil {
1763+
return nil, err
1764+
}
1765+
return &resp, nil
1766+
}
1767+
1768+
type DeleteDomainRequest struct {
1769+
Region scw.Region `json:"-"`
1770+
1771+
DomainID string `json:"-"`
1772+
}
1773+
1774+
func (s *API) DeleteDomain(req *DeleteDomainRequest, opts ...scw.RequestOption) (*Domain, error) {
1775+
var err error
1776+
1777+
if req.Region == "" {
1778+
defaultRegion, _ := s.client.GetDefaultRegion()
1779+
req.Region = defaultRegion
1780+
}
1781+
1782+
if fmt.Sprint(req.Region) == "" {
1783+
return nil, errors.New("field Region cannot be empty in request")
1784+
}
1785+
1786+
if fmt.Sprint(req.DomainID) == "" {
1787+
return nil, errors.New("field DomainID cannot be empty in request")
1788+
}
1789+
1790+
scwReq := &scw.ScalewayRequest{
1791+
Method: "DELETE",
1792+
Path: "/functions/v1beta1/regions/" + fmt.Sprint(req.Region) + "/domains/" + fmt.Sprint(req.DomainID) + "",
1793+
Headers: http.Header{},
1794+
}
1795+
1796+
var resp Domain
1797+
1798+
err = s.client.Do(scwReq, &resp, opts...)
1799+
if err != nil {
1800+
return nil, err
1801+
}
1802+
return &resp, nil
1803+
}
1804+
15381805
type IssueJWTRequest struct {
15391806
Region scw.Region `json:"-"`
15401807

@@ -1653,3 +1920,22 @@ func (r *ListLogsResponse) UnsafeAppend(res interface{}) (uint32, error) {
16531920
r.TotalCount += uint32(len(results.Logs))
16541921
return uint32(len(results.Logs)), nil
16551922
}
1923+
1924+
// UnsafeGetTotalCount should not be used
1925+
// Internal usage only
1926+
func (r *ListDomainsResponse) UnsafeGetTotalCount() uint32 {
1927+
return r.TotalCount
1928+
}
1929+
1930+
// UnsafeAppend should not be used
1931+
// Internal usage only
1932+
func (r *ListDomainsResponse) UnsafeAppend(res interface{}) (uint32, error) {
1933+
results, ok := res.(*ListDomainsResponse)
1934+
if !ok {
1935+
return 0, errors.New("%T type cannot be appended to type %T", res, r)
1936+
}
1937+
1938+
r.Domains = append(r.Domains, results.Domains...)
1939+
r.TotalCount += uint32(len(results.Domains))
1940+
return uint32(len(results.Domains)), nil
1941+
}

0 commit comments

Comments
 (0)