Skip to content

Commit 65b13d2

Browse files
authored
feat(webhosting): add first version of the classic mail api (#2165)
1 parent 0d8542e commit 65b13d2

File tree

1 file changed

+321
-0
lines changed

1 file changed

+321
-0
lines changed

api/webhosting/v1alpha1/webhosting_sdk.go

Lines changed: 321 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,15 @@ type HostingOption struct {
419419
Name string `json:"name"`
420420
}
421421

422+
// EmailAddress: email address.
423+
type EmailAddress struct {
424+
// Domain: domain part of the mailbox address.
425+
Domain string `json:"domain"`
426+
427+
// Login: username part address of the mailbox address.
428+
Login string `json:"login"`
429+
}
430+
422431
// OfferProduct: offer product.
423432
type OfferProduct struct {
424433
// Name: product name.
@@ -591,6 +600,15 @@ type Hosting struct {
591600
Region scw.Region `json:"region"`
592601
}
593602

603+
// Mailbox: mailbox.
604+
type Mailbox struct {
605+
// MailboxID: the ID of the mailbox.
606+
MailboxID uint32 `json:"mailbox_id"`
607+
608+
// Email: the email address of the mailbox.
609+
Email *EmailAddress `json:"email"`
610+
}
611+
594612
// Offer: offer.
595613
type Offer struct {
596614
// ID: offer ID.
@@ -636,6 +654,78 @@ type CheckUserOwnsDomainResponse struct {
636654
OwnsDomain bool `json:"owns_domain"`
637655
}
638656

657+
// ClassicMailAPICreateMailboxRequest: classic mail api create mailbox request.
658+
type ClassicMailAPICreateMailboxRequest struct {
659+
// Region: region to target. If none is passed will use default region from the config.
660+
Region scw.Region `json:"-"`
661+
662+
// OnlineID: the Online hosting ID.
663+
OnlineID uint32 `json:"-"`
664+
665+
// Email: the email address of the mailbox.
666+
Email *EmailAddress `json:"email,omitempty"`
667+
668+
// Password: password for the new mailbox.
669+
Password string `json:"password"`
670+
}
671+
672+
// ClassicMailAPIDeleteMailboxRequest: classic mail api delete mailbox request.
673+
type ClassicMailAPIDeleteMailboxRequest struct {
674+
// Region: region to target. If none is passed will use default region from the config.
675+
Region scw.Region `json:"-"`
676+
677+
// OnlineID: the Online hosting ID.
678+
OnlineID uint32 `json:"-"`
679+
680+
// MailboxID: the ID of the mailbox to delete.
681+
MailboxID uint32 `json:"-"`
682+
}
683+
684+
// ClassicMailAPIGetMailboxRequest: classic mail api get mailbox request.
685+
type ClassicMailAPIGetMailboxRequest struct {
686+
// Region: region to target. If none is passed will use default region from the config.
687+
Region scw.Region `json:"-"`
688+
689+
// OnlineID: the Online hosting ID.
690+
OnlineID uint32 `json:"-"`
691+
692+
// MailboxID: the ID of the mailbox to get.
693+
MailboxID uint32 `json:"-"`
694+
}
695+
696+
// ClassicMailAPIListMailboxesRequest: classic mail api list mailboxes request.
697+
type ClassicMailAPIListMailboxesRequest struct {
698+
// Region: region to target. If none is passed will use default region from the config.
699+
Region scw.Region `json:"-"`
700+
701+
// OnlineID: the Online hosting ID.
702+
OnlineID uint32 `json:"-"`
703+
704+
// Page: page number (must be a positive integer).
705+
Page *int32 `json:"-"`
706+
707+
// PageSize: number of mailboxes to return (must be a positive integer lower or equal to 100).
708+
PageSize *uint32 `json:"-"`
709+
710+
// Domain: domain to filter the mailboxes.
711+
Domain *string `json:"-"`
712+
}
713+
714+
// ClassicMailAPIUpdateMailboxRequest: classic mail api update mailbox request.
715+
type ClassicMailAPIUpdateMailboxRequest struct {
716+
// Region: region to target. If none is passed will use default region from the config.
717+
Region scw.Region `json:"-"`
718+
719+
// OnlineID: the Online hosting ID.
720+
OnlineID uint32 `json:"-"`
721+
722+
// MailboxID: the ID of the mailbox to update.
723+
MailboxID uint32 `json:"-"`
724+
725+
// Password: new password for the mailbox.
726+
Password *string `json:"password,omitempty"`
727+
}
728+
639729
// CreateHostingRequest: create hosting request.
640730
type CreateHostingRequest struct {
641731
// Region: region to target. If none is passed will use default region from the config.
@@ -818,6 +908,34 @@ func (r *ListHostingsResponse) UnsafeAppend(res interface{}) (uint32, error) {
818908
return uint32(len(results.Hostings)), nil
819909
}
820910

911+
// ListMailboxesResponse: list mailboxes response.
912+
type ListMailboxesResponse struct {
913+
// TotalCount: total number of mailboxes.
914+
TotalCount uint64 `json:"total_count"`
915+
916+
// Mailboxes: list of mailboxes.
917+
Mailboxes []*Mailbox `json:"mailboxes"`
918+
}
919+
920+
// UnsafeGetTotalCount should not be used
921+
// Internal usage only
922+
func (r *ListMailboxesResponse) UnsafeGetTotalCount() uint64 {
923+
return r.TotalCount
924+
}
925+
926+
// UnsafeAppend should not be used
927+
// Internal usage only
928+
func (r *ListMailboxesResponse) UnsafeAppend(res interface{}) (uint64, error) {
929+
results, ok := res.(*ListMailboxesResponse)
930+
if !ok {
931+
return 0, errors.New("%T type cannot be appended to type %T", res, r)
932+
}
933+
934+
r.Mailboxes = append(r.Mailboxes, results.Mailboxes...)
935+
r.TotalCount += uint64(len(results.Mailboxes))
936+
return uint64(len(results.Mailboxes)), nil
937+
}
938+
821939
// ListOffersRequest: list offers request.
822940
type ListOffersRequest struct {
823941
// Region: region to target. If none is passed will use default region from the config.
@@ -1345,3 +1463,206 @@ func (s *API) ResetHostingPassword(req *ResetHostingPasswordRequest, opts ...scw
13451463
}
13461464
return &resp, nil
13471465
}
1466+
1467+
// This API allows you to manage your mailboxes for your Web Hosting services.
1468+
type ClassicMailAPI struct {
1469+
client *scw.Client
1470+
}
1471+
1472+
// NewClassicMailAPI returns a ClassicMailAPI object from a Scaleway client.
1473+
func NewClassicMailAPI(client *scw.Client) *ClassicMailAPI {
1474+
return &ClassicMailAPI{
1475+
client: client,
1476+
}
1477+
}
1478+
func (s *ClassicMailAPI) Regions() []scw.Region {
1479+
return []scw.Region{scw.RegionFrPar, scw.RegionNlAms, scw.RegionPlWaw}
1480+
}
1481+
1482+
// CreateMailbox: Create a new mailbox within your hosting plan.
1483+
func (s *ClassicMailAPI) CreateMailbox(req *ClassicMailAPICreateMailboxRequest, opts ...scw.RequestOption) (*Mailbox, error) {
1484+
var err error
1485+
1486+
if req.Region == "" {
1487+
defaultRegion, _ := s.client.GetDefaultRegion()
1488+
req.Region = defaultRegion
1489+
}
1490+
1491+
if fmt.Sprint(req.Region) == "" {
1492+
return nil, errors.New("field Region cannot be empty in request")
1493+
}
1494+
1495+
if fmt.Sprint(req.OnlineID) == "" {
1496+
return nil, errors.New("field OnlineID cannot be empty in request")
1497+
}
1498+
1499+
scwReq := &scw.ScalewayRequest{
1500+
Method: "POST",
1501+
Path: "/webhosting/v1alpha1/regions/" + fmt.Sprint(req.Region) + "/classic-hostings/" + fmt.Sprint(req.OnlineID) + "/mailboxes",
1502+
}
1503+
1504+
err = scwReq.SetBody(req)
1505+
if err != nil {
1506+
return nil, err
1507+
}
1508+
1509+
var resp Mailbox
1510+
1511+
err = s.client.Do(scwReq, &resp, opts...)
1512+
if err != nil {
1513+
return nil, err
1514+
}
1515+
return &resp, nil
1516+
}
1517+
1518+
// GetMailbox: Get a mailbox by id within your hosting plan.
1519+
func (s *ClassicMailAPI) GetMailbox(req *ClassicMailAPIGetMailboxRequest, opts ...scw.RequestOption) (*Mailbox, error) {
1520+
var err error
1521+
1522+
if req.Region == "" {
1523+
defaultRegion, _ := s.client.GetDefaultRegion()
1524+
req.Region = defaultRegion
1525+
}
1526+
1527+
if fmt.Sprint(req.Region) == "" {
1528+
return nil, errors.New("field Region cannot be empty in request")
1529+
}
1530+
1531+
if fmt.Sprint(req.OnlineID) == "" {
1532+
return nil, errors.New("field OnlineID cannot be empty in request")
1533+
}
1534+
1535+
if fmt.Sprint(req.MailboxID) == "" {
1536+
return nil, errors.New("field MailboxID cannot be empty in request")
1537+
}
1538+
1539+
scwReq := &scw.ScalewayRequest{
1540+
Method: "GET",
1541+
Path: "/webhosting/v1alpha1/regions/" + fmt.Sprint(req.Region) + "/classic-hostings/" + fmt.Sprint(req.OnlineID) + "/mailboxes/" + fmt.Sprint(req.MailboxID) + "",
1542+
}
1543+
1544+
var resp Mailbox
1545+
1546+
err = s.client.Do(scwReq, &resp, opts...)
1547+
if err != nil {
1548+
return nil, err
1549+
}
1550+
return &resp, nil
1551+
}
1552+
1553+
// ListMailboxes: List all mailboxes within your hosting plan.
1554+
func (s *ClassicMailAPI) ListMailboxes(req *ClassicMailAPIListMailboxesRequest, opts ...scw.RequestOption) (*ListMailboxesResponse, error) {
1555+
var err error
1556+
1557+
if req.Region == "" {
1558+
defaultRegion, _ := s.client.GetDefaultRegion()
1559+
req.Region = defaultRegion
1560+
}
1561+
1562+
defaultPageSize, exist := s.client.GetDefaultPageSize()
1563+
if (req.PageSize == nil || *req.PageSize == 0) && exist {
1564+
req.PageSize = &defaultPageSize
1565+
}
1566+
1567+
query := url.Values{}
1568+
parameter.AddToQuery(query, "page", req.Page)
1569+
parameter.AddToQuery(query, "page_size", req.PageSize)
1570+
parameter.AddToQuery(query, "domain", req.Domain)
1571+
1572+
if fmt.Sprint(req.Region) == "" {
1573+
return nil, errors.New("field Region cannot be empty in request")
1574+
}
1575+
1576+
if fmt.Sprint(req.OnlineID) == "" {
1577+
return nil, errors.New("field OnlineID cannot be empty in request")
1578+
}
1579+
1580+
scwReq := &scw.ScalewayRequest{
1581+
Method: "GET",
1582+
Path: "/webhosting/v1alpha1/regions/" + fmt.Sprint(req.Region) + "/classic-hostings/" + fmt.Sprint(req.OnlineID) + "/mailboxes",
1583+
Query: query,
1584+
}
1585+
1586+
var resp ListMailboxesResponse
1587+
1588+
err = s.client.Do(scwReq, &resp, opts...)
1589+
if err != nil {
1590+
return nil, err
1591+
}
1592+
return &resp, nil
1593+
}
1594+
1595+
// DeleteMailbox:
1596+
func (s *ClassicMailAPI) DeleteMailbox(req *ClassicMailAPIDeleteMailboxRequest, opts ...scw.RequestOption) (*Mailbox, error) {
1597+
var err error
1598+
1599+
if req.Region == "" {
1600+
defaultRegion, _ := s.client.GetDefaultRegion()
1601+
req.Region = defaultRegion
1602+
}
1603+
1604+
if fmt.Sprint(req.Region) == "" {
1605+
return nil, errors.New("field Region cannot be empty in request")
1606+
}
1607+
1608+
if fmt.Sprint(req.OnlineID) == "" {
1609+
return nil, errors.New("field OnlineID cannot be empty in request")
1610+
}
1611+
1612+
if fmt.Sprint(req.MailboxID) == "" {
1613+
return nil, errors.New("field MailboxID cannot be empty in request")
1614+
}
1615+
1616+
scwReq := &scw.ScalewayRequest{
1617+
Method: "DELETE",
1618+
Path: "/webhosting/v1alpha1/regions/" + fmt.Sprint(req.Region) + "/classic-hostings/" + fmt.Sprint(req.OnlineID) + "/mailboxes/" + fmt.Sprint(req.MailboxID) + "",
1619+
}
1620+
1621+
var resp Mailbox
1622+
1623+
err = s.client.Do(scwReq, &resp, opts...)
1624+
if err != nil {
1625+
return nil, err
1626+
}
1627+
return &resp, nil
1628+
}
1629+
1630+
// UpdateMailbox: Update the mailbox within your hosting plan.
1631+
func (s *ClassicMailAPI) UpdateMailbox(req *ClassicMailAPIUpdateMailboxRequest, opts ...scw.RequestOption) (*Mailbox, error) {
1632+
var err error
1633+
1634+
if req.Region == "" {
1635+
defaultRegion, _ := s.client.GetDefaultRegion()
1636+
req.Region = defaultRegion
1637+
}
1638+
1639+
if fmt.Sprint(req.Region) == "" {
1640+
return nil, errors.New("field Region cannot be empty in request")
1641+
}
1642+
1643+
if fmt.Sprint(req.OnlineID) == "" {
1644+
return nil, errors.New("field OnlineID cannot be empty in request")
1645+
}
1646+
1647+
if fmt.Sprint(req.MailboxID) == "" {
1648+
return nil, errors.New("field MailboxID cannot be empty in request")
1649+
}
1650+
1651+
scwReq := &scw.ScalewayRequest{
1652+
Method: "PATCH",
1653+
Path: "/webhosting/v1alpha1/regions/" + fmt.Sprint(req.Region) + "/classic-hostings/" + fmt.Sprint(req.OnlineID) + "/mailboxes/" + fmt.Sprint(req.MailboxID) + "",
1654+
}
1655+
1656+
err = scwReq.SetBody(req)
1657+
if err != nil {
1658+
return nil, err
1659+
}
1660+
1661+
var resp Mailbox
1662+
1663+
err = s.client.Do(scwReq, &resp, opts...)
1664+
if err != nil {
1665+
return nil, err
1666+
}
1667+
return &resp, nil
1668+
}

0 commit comments

Comments
 (0)