Skip to content

Commit

Permalink
Security support(Network Based Single Device Only) (#7)
Browse files Browse the repository at this point in the history
Add network based security support

- Single FW/UTM
- WAF
  • Loading branch information
keiichi-hikita authored Aug 8, 2019
1 parent 23fa87c commit 23315b5
Show file tree
Hide file tree
Showing 45 changed files with 1,734 additions and 12 deletions.
16 changes: 16 additions & 0 deletions ecl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,19 @@ func NewContainerInfraV1(client *eclcloud.ProviderClient, eo eclcloud.EndpointOp
func NewWorkflowV2(client *eclcloud.ProviderClient, eo eclcloud.EndpointOpts) (*eclcloud.ServiceClient, error) {
return initClientOpts(client, eo, "workflowv2")
}

// NewSecurityOrderV1 creates a ServiceClient that may be used to access the v1 Security
// Order API service.
func NewSecurityOrderV1(client *eclcloud.ProviderClient, eo eclcloud.EndpointOpts) (*eclcloud.ServiceClient, error) {
sc, err := initClientOpts(client, eo, "mss-rfg")
// sc.ResourceBase = sc.Endpoint + "v2/"
return sc, err
}

// NewSecurityPortalV1 creates a ServiceClient that may be used to access the v1 Security
// Portal API service.
func NewSecurityPortalV1(client *eclcloud.ProviderClient, eo eclcloud.EndpointOpts) (*eclcloud.ServiceClient, error) {
sc, err := initClientOpts(client, eo, "mss-msa")
// sc.ResourceBase = sc.Endpoint + "v2/"
return sc, err
}
1 change: 0 additions & 1 deletion ecl/dns/v2/zones/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ func (r *Zone) UnmarshalJSON(b []byte) error {

r.CreatedAt = time.Time(s.CreatedAt)
r.UpdatedAt = time.Time(s.UpdatedAt)
// log.Printf("[MYDEBUG: s.TransfeerdAt %#v", s.TransferredAt)
r.TransferredAt = time.Time(s.TransferredAt)

switch t := s.Serial.(type) {
Expand Down
2 changes: 2 additions & 0 deletions ecl/security_order/v1/network_based_device_single/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package network_based_device_single contains single device functionality on security.
package network_based_device_single
154 changes: 154 additions & 0 deletions ecl/security_order/v1/network_based_device_single/requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package network_based_device_single

import (
"github.com/nttcom/eclcloud"
"github.com/nttcom/eclcloud/pagination"
)

// ListOptsBuilder allows extensions to add additional parameters to
// the List request
type ListOptsBuilder interface {
ToSingleDeviceQuery() (string, error)
}

// ListOpts enables filtering of a list request.
type ListOpts struct {
TenantID string `q:"tenant_id"`
Locale string `q:"locale"`
}

// ToSingleDeviceQuery formats a ListOpts into a query string.
func (opts ListOpts) ToSingleDeviceQuery() (string, error) {
q, err := eclcloud.BuildQueryString(opts)
return q.String(), err
}

// List enumerates the Devices to which the current token has access.
func List(client *eclcloud.ServiceClient, deviceType string, opts ListOptsBuilder) pagination.Pager {
url := listURL(client, deviceType)
if opts != nil {
query, err := opts.ToSingleDeviceQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return SingleDevicePage{pagination.LinkedPageBase{PageResult: r}}
})
}

// CreateOptsBuilder allows extensions to add additional parameters to
// the Create request.
type CreateOptsBuilder interface {
ToSingleDeviceCreateMap() (map[string]interface{}, error)
}

// GtHostInCreate represents parameters used to create a Single Device.
type GtHostInCreate struct {
OperatingMode string `json:"operatingmode" required:"true"`
LicenseKind string `json:"licensekind" required:"true"`
AZGroup string `json:"azgroup" required:"true"`
}

// CreateOpts represents parameters used to create a device.
type CreateOpts struct {
SOKind string `json:"sokind" required:"true"`
TenantID string `json:"tenant_id" required:"true"`
Locale string `json:"locale,omitempty"`
GtHost [1]GtHostInCreate `json:"gt_host" required:"true"`
}

// ToSingleDeviceCreateMap formats a CreateOpts into a create request.
func (opts CreateOpts) ToSingleDeviceCreateMap() (map[string]interface{}, error) {
return eclcloud.BuildRequestBody(opts, "")
}

// Create creates a new device.
func Create(client *eclcloud.ServiceClient, deviceType string, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToSingleDeviceCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(createURL(client, deviceType), &b, &r.Body, &eclcloud.RequestOpts{
OkCodes: []int{200},
})
return
}

// DeleteOptsBuilder allows extensions to add additional parameters to
// the Delete request.
type DeleteOptsBuilder interface {
ToSingleDeviceDeleteMap() (map[string]interface{}, error)
}

// GtHostInDelete represents parameters used to delete a Single Device.
type GtHostInDelete struct {
HostName string `json:"hostname" required:"true"`
}

// DeleteOpts represents parameters used to delete a device.
type DeleteOpts struct {
SOKind string `json:"sokind" required:"true"`
TenantID string `json:"tenant_id" required:"true"`
GtHost [1]GtHostInDelete `json:"gt_host" required:"true"`
}

// ToSingleDeviceDeleteMap formats a DeleteOpts into a delete request.
func (opts DeleteOpts) ToSingleDeviceDeleteMap() (map[string]interface{}, error) {
return eclcloud.BuildRequestBody(opts, "")
}

// Delete deletes a device.
func Delete(client *eclcloud.ServiceClient, deviceType string, opts DeleteOptsBuilder) (r DeleteResult) {
b, err := opts.ToSingleDeviceDeleteMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(createURL(client, deviceType), &b, &r.Body, &eclcloud.RequestOpts{
OkCodes: []int{200},
})
return

}

// UpdateOptsBuilder allows extensions to add additional parameters to
// the Update request.
type UpdateOptsBuilder interface {
ToSingleDeviceUpdateMap() (map[string]interface{}, error)
}

// GtHostInUpdate represents parameters used to update a Single Device.
type GtHostInUpdate struct {
OperatingMode string `json:"operatingmode" required:"true"`
LicenseKind string `json:"licensekind" required:"true"`
HostName string `json:"hostname" required:"true"`
}

// UpdateOpts represents parameters to update a Single Device.
type UpdateOpts struct {
SOKind string `json:"sokind" required:"true"`
Locale string `json:"locale,omitempty"`
TenantID string `json:"tenant_id" required:"true"`
GtHost [1]GtHostInUpdate `json:"gt_host" required:"true"`
}

// ToSingleDeviceUpdateMap formats a UpdateOpts into an update request.
func (opts UpdateOpts) ToSingleDeviceUpdateMap() (map[string]interface{}, error) {
return eclcloud.BuildRequestBody(opts, "")
}

// Update modifies the attributes of a device.
func Update(client *eclcloud.ServiceClient, deviceType string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.ToSingleDeviceUpdateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(updateURL(client, deviceType), &b, &r.Body, &eclcloud.RequestOpts{
OkCodes: []int{200},
})
return
}
104 changes: 104 additions & 0 deletions ecl/security_order/v1/network_based_device_single/results.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package network_based_device_single

import (
"github.com/nttcom/eclcloud"
"github.com/nttcom/eclcloud/pagination"
)

type commonResult struct {
eclcloud.Result
}

// Extract is a function that accepts a result
// and extracts a Single Device resource.
func (r commonResult) Extract() (*SingleDeviceOrder, error) {
var sdo SingleDeviceOrder
err := r.ExtractInto(&sdo)
return &sdo, err
}

// Extract interprets any commonResult as a Single Device if possible.
func (r commonResult) ExtractInto(v interface{}) error {
return r.Result.ExtractIntoStructPtr(v, "")
}

// CreateResult represents the result of a create operation. Call its Extract
// method to interpret it as a Single Device.
type CreateResult struct {
commonResult
}

// GetResult represents the result of a get operation. Call its Extract
// method to interpret it as a Single Device.
type GetResult struct {
commonResult
}

// UpdateResult represents the result of an update operation. Call its Extract
// method to interpret it as a Single Device.
type UpdateResult struct {
commonResult
}

// DeleteResult represents the result of a delete operation. Call its
// ExtractErr method to determine if the request succeeded or failed.
type DeleteResult struct {
commonResult
}

// SingleDevice represents the result of a each element in
// response of single device api result.
type SingleDevice struct {
ID int `json:"id"`
Cell []string `json:"cell"`
}

// SingleDeviceOrder represents a Single Device's each order.
type SingleDeviceOrder struct {
ID string `json:"soId"`
Code string `json:"code"`
Message string `json:"message"`
Status int `json:"status"`
}

// SingleDevicePage is the page returned by a pager
// when traversing over a collection of Single Device.
type SingleDevicePage struct {
pagination.LinkedPageBase
}

// NextPageURL is invoked when a paginated collection of Single Device
// has reached the end of a page and the pager seeks to traverse over a new one.
// In order to do this, it needs to construct the next page's URL.
func (r SingleDevicePage) NextPageURL() (string, error) {
var s struct {
Links []eclcloud.Link `json:"single_firewall_utm_links"`
}
err := r.ExtractInto(&s)
if err != nil {
return "", err
}
return eclcloud.ExtractNextURL(s.Links)
}

// IsEmpty checks whether a SingleDevicePage struct is empty.
func (r SingleDevicePage) IsEmpty() (bool, error) {
is, err := ExtractSingleDevices(r)
return len(is) == 0, err
}

// ExtractSingleDevices accepts a Page struct,
// specifically a SingleDevicePage struct, and extracts the elements
// into a slice of Single Device structs.
// In other words, a generic collection is mapped into a relevant slice.
func ExtractSingleDevices(r pagination.Page) ([]SingleDevice, error) {
var s []SingleDevice
err := ExtractSingleDevicesInto(r, &s)
return s, err
}

// ExtractSingleDevicesInto interprets the results of a single page from a List() call,
// producing a slice of Device entities.
func ExtractSingleDevicesInto(r pagination.Page, v interface{}) error {
return r.(SingleDevicePage).Result.ExtractIntoSlicePtr(v, "rows")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package testing contains network based security single device unittests
package testing
Loading

0 comments on commit 23315b5

Please sign in to comment.