Skip to content

Commit

Permalink
feat(partner): add Partner API support (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
ucmase authored Dec 10, 2024
1 parent 9401bd7 commit e1a6fb3
Show file tree
Hide file tree
Showing 10 changed files with 313 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
repos:
- repo: https://github.com/golangci/golangci-lint
rev: v1.50.1
rev: v1.62.2
hooks:
- id: golangci-lint
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ See updating [Changelog example here](https://keepachangelog.com/en/1.0.0/)

## [Unreleased]

### Added
- support for Partner API

## [8.13.0]

### Added
Expand Down
17 changes: 17 additions & 0 deletions upcloud/partner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package upcloud

// PartnerAccount represents details of an account associated with a partner
type PartnerAccount struct {
Username string `json:"username"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Country string `json:"country"`
State string `json:"state"`
Email string `json:"email"`
Phone string `json:"phone"`
Company string `json:"company"`
Address string `json:"address"`
PostalCode string `json:"postal_code"`
City string `json:"city"`
VATNumber string `json:"vat_number"`
}
47 changes: 47 additions & 0 deletions upcloud/partner_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package upcloud

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
)

func TestUnmarshalPartnerAccount(t *testing.T) {
originalJSON := `
{
"address" : "Some street",
"city" : "Some city",
"company" : "Some company",
"country" : "FIN",
"email" : "[email protected]",
"first_name" : "Some",
"last_name" : "User",
"phone" : "+358.91234567",
"postal_code" : "00100",
"state" : "",
"username" : "someuser",
"vat_number" : ""
}
`

expectedAccount := PartnerAccount{
Username: "someuser",
FirstName: "Some",
LastName: "User",
Country: "FIN",
State: "",
Email: "[email protected]",
Phone: "+358.91234567",
Company: "Some company",
Address: "Some street",
PostalCode: "00100",
City: "Some city",
VATNumber: "",
}

account := PartnerAccount{}
err := json.Unmarshal([]byte(originalJSON), &account)
assert.NoError(t, err)
assert.Equal(t, expectedAccount, account)
}
27 changes: 27 additions & 0 deletions upcloud/request/partner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package request

// CreatePartnerAccountContactDetails represents optional contact details in CreatePartnerAccountRequest
type CreatePartnerAccountContactDetails struct {
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
Country string `json:"country,omitempty"`
State string `json:"state,omitempty"`
Email string `json:"email,omitempty"`
Phone string `json:"phone,omitempty"`
Company string `json:"company,omitempty"`
Address string `json:"address,omitempty"`
PostalCode string `json:"postal_code,omitempty"`
City string `json:"city,omitempty"`
VATNumber string `json:"vat_number,omitempty"`
}

// CreatePartnerAccountRequest represents a request to create new main account for partner
type CreatePartnerAccountRequest struct {
Username string `json:"username"`
Password string `json:"password"`
ContactDetails *CreatePartnerAccountContactDetails `json:"contact_details,omitempty"`
}

func (r *CreatePartnerAccountRequest) RequestURL() string {
return "/partner/accounts"
}
56 changes: 56 additions & 0 deletions upcloud/request/partner_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package request

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
)

func TestCreatePartnerAccountRequestWithoutContactDetails(t *testing.T) {
request := CreatePartnerAccountRequest{
Username: "someuser",
Password: "superSecret123",
}
expectedJSON := `
{
"username" : "someuser",
"password" : "superSecret123"
}
`
actualJSON, err := json.Marshal(&request)
assert.NoError(t, err)
assert.JSONEq(t, expectedJSON, string(actualJSON))
assert.Equal(t, "/partner/accounts", request.RequestURL())
}

func TestCreatePartnerAccountRequestWithMinimalContactDetails(t *testing.T) {
request := CreatePartnerAccountRequest{
Username: "someuser",
Password: "superSecret123",
ContactDetails: &CreatePartnerAccountContactDetails{
FirstName: "Some",
LastName: "User",
Country: "FIN",
Email: "[email protected]",
Phone: "+358.91234567",
},
}
expectedJSON := `
{
"username" : "someuser",
"password" : "superSecret123",
"contact_details" : {
"first_name" : "Some",
"last_name" : "User",
"country" : "FIN",
"email" : "[email protected]",
"phone" : "+358.91234567"
}
}
`
actualJSON, err := json.Marshal(&request)
assert.NoError(t, err)
assert.JSONEq(t, expectedJSON, string(actualJSON))
assert.Equal(t, "/partner/accounts", request.RequestURL())
}
85 changes: 85 additions & 0 deletions upcloud/service/fixtures/createpartneraccount.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
---
version: 1
interactions:
- request:
body: '{"username":"sdk_test_partner_account","password":"superSecret123","contact_details":{"first_name":"Test","last_name":"User","country":"FIN","email":"[email protected]","phone":"+358.31245434"}}'
form: {}
headers:
Accept:
- application/json
Content-Type:
- application/json
User-Agent:
- upcloud-go-api/8.12.0
url: https://api.upcloud.com/1.3/partner/accounts
method: POST
response:
body: |
{
"address" : "",
"city" : "",
"company" : "",
"country" : "FIN",
"email" : "[email protected]",
"first_name" : "Test",
"last_name" : "User",
"phone" : "+358.31245434",
"postal_code" : "",
"state" : "",
"username" : "sdk_test_partner_account",
"vat_number" : ""
}
headers:
Content-Length:
- "303"
Content-Type:
- application/json; charset=UTF-8
Date:
- Tue, 03 Dec 2024 14:54:50 GMT
Server:
- Apache
status: 201 Created
code: 201
duration: ""
- request:
body: ""
form: {}
headers:
Accept:
- application/json
Content-Type:
- application/json
User-Agent:
- upcloud-go-api/8.12.0
url: https://api.upcloud.com/1.3/partner/accounts
method: GET
response:
body: |
[
{
"address" : "",
"city" : "",
"company" : "",
"country" : "FIN",
"email" : "[email protected]",
"first_name" : "Test",
"last_name" : "User",
"phone" : "+358.31245434",
"postal_code" : "",
"state" : "",
"username" : "sdk_test_partner_account",
"vat_number" : ""
}
]
headers:
Content-Length:
- "349"
Content-Type:
- application/json; charset=UTF-8
Date:
- Tue, 03 Dec 2024 14:54:51 GMT
Server:
- Apache
status: 200 OK
code: 200
duration: ""
25 changes: 25 additions & 0 deletions upcloud/service/partner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package service

import (
"context"

"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
)

type Partner interface {
CreatePartnerAccount(ctx context.Context, r *request.CreatePartnerAccountRequest) (*upcloud.PartnerAccount, error)
GetPartnerAccounts(ctx context.Context) ([]upcloud.PartnerAccount, error)
}

// CreatePartnerAccount creates new main account for partner
func (s *Service) CreatePartnerAccount(ctx context.Context, r *request.CreatePartnerAccountRequest) (*upcloud.PartnerAccount, error) {
partnerAccount := upcloud.PartnerAccount{}
return &partnerAccount, s.create(ctx, r, &partnerAccount)
}

// GetPartnerAccounts lists accounts associated with partner
func (s *Service) GetPartnerAccounts(ctx context.Context) ([]upcloud.PartnerAccount, error) {
accounts := make([]upcloud.PartnerAccount, 0)
return accounts, s.get(ctx, "/partner/accounts", &accounts)
}
51 changes: 51 additions & 0 deletions upcloud/service/partner_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package service

import (
"context"
"testing"

"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
"github.com/dnaeon/go-vcr/recorder"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCreatePartnerAccount(t *testing.T) {
record(t, "createpartneraccount", func(ctx context.Context, t *testing.T, rec *recorder.Recorder, svc *Service) {
username := "sdk_test_partner_account"
expectedAccount := upcloud.PartnerAccount{
Username: username,
FirstName: "Test",
LastName: "User",
Country: "FIN",
Email: "[email protected]",
Phone: "+358.31245434",
}

account, err := svc.CreatePartnerAccount(ctx, &request.CreatePartnerAccountRequest{
Username: username,
Password: "superSecret123",
ContactDetails: &request.CreatePartnerAccountContactDetails{
FirstName: expectedAccount.FirstName,
LastName: expectedAccount.LastName,
Country: expectedAccount.Country,
Email: expectedAccount.Email,
Phone: expectedAccount.Phone,
},
})
require.NoError(t, err)
assert.Equal(t, expectedAccount, *account)

accounts, err := svc.GetPartnerAccounts(ctx)
require.NoError(t, err)
accountFound := false
for _, a := range accounts {
if a.Username == username {
accountFound = true
assert.Equal(t, expectedAccount, a)
}
}
assert.True(t, accountFound)
})
}
1 change: 1 addition & 0 deletions upcloud/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type service interface {
Kubernetes
ManagedObjectStorage
Gateway
Partner
}

var _ service = (*Service)(nil)
Expand Down

0 comments on commit e1a6fb3

Please sign in to comment.