Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[db/v1/instances] add access field - trove database dbaas #2876

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions openstack/db/v1/instances/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ func (opts NetworkOpts) ToMap() (map[string]interface{}, error) {
return gophercloud.BuildRequestBody(opts, "")
}

// AccessOpts is used within CreateOpts to control database service exposing.
type AccessOpts struct {
// Whether the database service is exposed to the public.
// Optional.
IsPublic bool `json:"is_public,omitempty"`
// A list of IPv4, IPv6 or mix of both CIDRs that restrict access to the database service.
// 0.0.0.0/0 is used by default if this parameter is not provided.
// Optional.
AllowedCIDR []string `json:"allowed_cidrs,omitempty"`
}

// ToMap converts a AccessOpts to a map[string]string (for a request body).
func (opts AccessOpts) ToMap() (map[string]interface{}, error) {
return gophercloud.BuildRequestBody(opts, "")
}

// CreateOpts is the struct responsible for configuring a new database instance.
type CreateOpts struct {
// The availability zone of the instance.
Expand All @@ -69,6 +85,8 @@ type CreateOpts struct {
Datastore *DatastoreOpts
// Networks dictates how this server will be attached to available networks.
Networks []NetworkOpts
// A access object defines how the database service is exposed. Optional.
Access AccessOpts `json:"access,omitempty"`
}

// ToInstanceCreateMap will render a JSON map.
Expand Down Expand Up @@ -130,6 +148,15 @@ func (opts CreateOpts) ToInstanceCreateMap() (map[string]interface{}, error) {
instance["nics"] = networks
}

// Add check for opts.Access
if opts.Access.IsPublic || len(opts.Access.AllowedCIDR) > 0 {
access, err := opts.Access.ToMap()
if err != nil {
return nil, err
}
instance["access"] = access
}

volume := map[string]interface{}{
"size": opts.Size,
}
Expand Down
3 changes: 3 additions & 0 deletions openstack/db/v1/instances/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ type Instance struct {

// The instance addresses
Addresses []Address

// Controls database service exposing.
Access AccessOpts
}

func (r *Instance) UnmarshalJSON(b []byte) error {
Expand Down
22 changes: 22 additions & 0 deletions openstack/db/v1/instances/testing/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,15 @@ var instance = `
"updated": "` + timestamp + `",
"volume": {
"size": 2
},
"access": {
"is_public": true,
"allowed_cidrs": ["202.78.240.0/24"]
}
}
`

// Ref: https://github.com/openstack/trove/blob/40fdb7b44fb33e022b77ba8df63fde2565c70dea/api-ref/source/samples/instance-create-response.json
var instanceGet = `
{
"created": "` + timestamp + `",
Expand Down Expand Up @@ -88,6 +93,10 @@ var instanceGet = `
"size": 1,
"used": 0.12
},
"access": {
"is_public": true,
"allowed_cidrs": ["202.78.240.0/24"]
},
"addresses": [
{
"address": "10.1.0.62",
Expand All @@ -101,6 +110,7 @@ var instanceGet = `
}
`

// Ref: https://github.com/openstack/trove/blob/40fdb7b44fb33e022b77ba8df63fde2565c70dea/api-ref/source/samples/instance-create-request.json
var createReq = `
{
"instance": {
Expand Down Expand Up @@ -131,6 +141,10 @@ var createReq = `
"volume": {
"size": 2,
"type": "ssd"
},
"access": {
"is_public": true,
"allowed_cidrs": ["202.78.240.0/24"]
}
}
}
Expand Down Expand Up @@ -226,6 +240,10 @@ var expectedInstance = instances.Instance{
Type: "mysql",
Version: "5.6",
},
Access: instances.AccessOpts{
IsPublic: true,
AllowedCIDR: []string{"202.78.240.0/24"},
},
}

var expectedGetInstance = instances.Instance{
Expand Down Expand Up @@ -253,6 +271,10 @@ var expectedGetInstance = instances.Instance{
{Type: "private", Address: "10.1.0.62"},
{Type: "public", Address: "172.24.5.114"},
},
Access: instances.AccessOpts{
IsPublic: true,
AllowedCIDR: []string{"202.78.240.0/24"},
},
}

var expectedInstanceWithFault = instances.Instance{
Expand Down
8 changes: 8 additions & 0 deletions openstack/db/v1/instances/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func TestCreate(t *testing.T) {
},
Size: 2,
VolumeType: "ssd",
Access: instances.AccessOpts{
IsPublic: true,
AllowedCIDR: []string{"202.78.240.0/24"},
},
}

instance, err := instances.Create(fake.ServiceClient(), opts).Extract()
Expand Down Expand Up @@ -67,6 +71,10 @@ func TestCreateWithFault(t *testing.T) {
},
Size: 2,
VolumeType: "ssd",
Access: instances.AccessOpts{
IsPublic: true,
AllowedCIDR: []string{"202.78.240.0/24"},
},
}

instance, err := instances.Create(fake.ServiceClient(), opts).Extract()
Expand Down
Loading