Skip to content

Commit

Permalink
Resell V2: add auto_quotas parameter for projects (#88)
Browse files Browse the repository at this point in the history
Add AutoQuotas parameter to the projects.CreateOpts structure with unit
test.
  • Loading branch information
ozerovandrei committed Sep 25, 2018
1 parent 4a6cb27 commit 42f093c
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 9 deletions.
27 changes: 18 additions & 9 deletions selvpcclient/resell/v2/projects/requests_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,41 @@ type CreateOpts struct {

// Quotas sets quotas for a new project.
Quotas []quotas.QuotaOpts `json:"-"`

// AutoQuotas allows to automatically set quotas for the new project.
// Quota values will be calculated in the Resell V2 service.
AutoQuotas bool `json:"-"`
}

// MarshalJSON implements custom marshalling method for the CreateOpts.
func (opts *CreateOpts) MarshalJSON() ([]byte, error) {
// Return create options with name only if quotas isn't provided.
// Return create options with only name and auto_quotas parameters if quotas
// parameter hadn't been provided.
if len(opts.Quotas) == 0 {
return json.Marshal(&struct {
Name string `json:"name"`
Name string `json:"name"`
AutoQuotas bool `json:"auto_quotas"`
}{
Name: opts.Name,
Name: opts.Name,
AutoQuotas: opts.AutoQuotas,
})
}

// Convert opts's quotas update options slice to a map that has resource names
// as keys and resource quotas update options as values.
// Convert opts's quotas update options slice to a map that has resource
// names as keys and resource quotas update options as values.
quotasMap := make(map[string][]quotas.ResourceQuotaOpts, len(opts.Quotas))
for _, quota := range opts.Quotas {
quotasMap[quota.Name] = quota.ResourceQuotasOpts
}

return json.Marshal(&struct {
Name string `json:"name"`
Quotas map[string][]quotas.ResourceQuotaOpts `json:"quotas"`
Name string `json:"name"`
AutoQuotas bool `json:"auto_quotas"`
Quotas map[string][]quotas.ResourceQuotaOpts `json:"quotas"`
}{
Name: opts.Name,
Quotas: quotasMap,
Name: opts.Name,
AutoQuotas: opts.AutoQuotas,
Quotas: quotasMap,
})
}

Expand Down
63 changes: 63 additions & 0 deletions selvpcclient/resell/v2/projects/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,69 @@ var TestCreateProjectResponse = &projects.Project{
Enabled: true,
}

// TestCreateProjectAutoQuotasOptsRaw represents marshalled options for the
// Create request with auto_quotas parameter.
const TestCreateProjectAutoQuotasOptsRaw = `
{
"project": {
"name": "Project2",
"auto_quotas": true
}
}
`

// TestCreateProjectAutoQuotasOpts represent options for the Create request
// with auto_quotas parameter.
var TestCreateProjectAutoQuotasOpts = projects.CreateOpts{
Name: "Project2",
AutoQuotas: true,
}

// TestCreateProjectAutoQuotasResponseRaw represents a raw response from the
// Create request with auto_quotas parameter.
const TestCreateProjectAutoQuotasResponseRaw = `
{
"project": {
"enabled": true,
"id": "9c97bdc75295493096cf5edcb8c37933",
"name": "Project2",
"url": "https://yyyyyy.selvpc.ru",
"quotas": {
"compute_cores": [
{
"region": "ru-1",
"used": 2,
"value": 10,
"zone": "ru-1b"
}
]
}
}
}
`

// TestCreateProjectAutoQuotasResponse represents the unmarshalled
// TestCreateProjectAutoQuotasResponseRaw response.
var TestCreateProjectAutoQuotasResponse = &projects.Project{
ID: "9c97bdc75295493096cf5edcb8c37933",
Name: "Project2",
URL: "https://yyyyyy.selvpc.ru",
Enabled: true,
Quotas: []quotas.Quota{
{
Name: "compute_cores",
ResourceQuotasEntities: []quotas.ResourceQuotaEntity{
{
Region: "ru-1",
Used: 2,
Value: 10,
Zone: "ru-1b",
},
},
},
},
}

// TestUpdateProjectOptsRaw represents marshalled options for the Update request.
const TestUpdateProjectOptsRaw = `
{
Expand Down
27 changes: 27 additions & 0 deletions selvpcclient/resell/v2/projects/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,33 @@ func TestCreateProject(t *testing.T) {
}
}

func TestCreateProjectAutoQuotas(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/projects",
TestCreateProjectAutoQuotasResponseRaw, TestCreateProjectAutoQuotasOptsRaw,
http.MethodPost, http.StatusOK, &endpointCalled, t)

ctx := context.Background()
createOpts := TestCreateProjectAutoQuotasOpts
actualResponse, _, err := projects.Create(ctx, testEnv.Client, createOpts)
if err != nil {
t.Fatal(err)
}

expectedResponse := TestCreateProjectAutoQuotasResponse

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if !reflect.DeepEqual(actualResponse, expectedResponse) {
t.Fatalf("expected %#v, but got %#v", actualResponse, expectedResponse)
}
}

func TestCreateProjectsHTTPError(t *testing.T) {
endpointCalled := false

Expand Down

0 comments on commit 42f093c

Please sign in to comment.