Skip to content

Commit

Permalink
Merge pull request #1 from nttcom/htysh/IF-4274_Support_connection_Azure
Browse files Browse the repository at this point in the history
IF-4274 Support connection Azure
  • Loading branch information
htysh authored Dec 17, 2019
2 parents ec2ae69 + 964748c commit f7f527f
Show file tree
Hide file tree
Showing 30 changed files with 3,278 additions and 0 deletions.
101 changes: 101 additions & 0 deletions fic/eri/v1/port_to_azure_microsoft_connections/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
Package ports contains functionality for working with
FIC Connection resources.
Example to List Connections
import (
con "github.com/nttcom/go-fic/fic/eri/v1/port_to_azure_microsoft_connections"
)
listOpts := con.ListOpts{}
allPages, err := con.List(fakeclient.ServiceClient(), nil).AllPages()
if err != nil {
panic(err)
}
allConnections, err := con.ExtractPorts(allPages)
if err != nil {
panic(err)
}
for _, c := range allConnections {
fmt.Printf("%+v", c)
}
Example to Get Connection
connectionID := "484cda0e-106f-4f4b-bb3f-d413710bbe78"
c, err := con.Get(fakeclient.ServiceClient(), connectionID).Extract()
if err != nil {
panic(err)
}
Example to Create a Connection
createOpts := con.CreateOpts{
Name: "YourConnectionName",
Source: con.Source{
Primary: con.Primary{
PortID: "F010123456789",
VLAN: 1025,
},
Secondary: con.Secondary{
PortID: "F019876543210",
VLAN: 1057,
},
ASN: "65530",
},
Destination: con.Destination{
Interconnect: "Tokyo-1",
ServiceKey: "6191af11-82f9-4c15-9894-9a69c8f8628a",
SharedKey: "a8268f8c96a9",
QosType: "guarantee",
AdvertisedPublicPrefixes: []string{
"100.100.1.1/32",
"100.100.1.2/32",
},
RoutingRegistryName: "APNIC",
},
Bandwidth: "100M",
PrimaryConnectedNetworkAddress: "10.10.0.0/30",
SecondaryConnectedNetworkAddress: "10.20.0.0/30",
}
c, err := con.Create(fakeclient.ServiceClient(), createOpts).Extract()
if err != nil {
panic(err)
}
Example to Delete a Connection
connectionID := "484cda0e-106f-4f4b-bb3f-d413710bbe78"
res := con.Delete(fakeclient.ServiceClient(), connectionID)
if err != nil {
panic(err)
}
Example to Update a Connection
updateOpts := con.UpdateOpts{
Destination: con.DestinationForUpdate{
AdvertisedPublicPrefixes: []string{
"100.100.1.1/32",
"100.100.1.2/32",
"100.100.1.3/32",
},
RoutingRegistryName: "ARIN",
},
}
connectionID := "484cda0e-106f-4f4b-bb3f-d413710bbe78"
c, err := con.Update(fakeclient.ServiceClient(), connectionID, updateOpts).Extract()
if err != nil {
panic(err)
}
*/
package port_to_azure_microsoft_connections
156 changes: 156 additions & 0 deletions fic/eri/v1/port_to_azure_microsoft_connections/requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package port_to_azure_microsoft_connections

import (
fic "github.com/nttcom/go-fic"
"github.com/nttcom/go-fic/pagination"
)

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

// ListOpts allows the filtering and sorting of paginated collections through
// the API. Filtering is achieved by passing in struct field values that map to
// the connection attributes you want to see returned.
type ListOpts struct {
}

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

// List returns a Pager which allows you to iterate over
// a collection of connections.
// It accepts a ListOpts struct, which allows you to filter and sort
// the returned collection for greater efficiency.
func List(c *fic.ServiceClient, opts ListOptsBuilder) pagination.Pager {
url := listURL(c)
if opts != nil {
query, err := opts.ToConnectionListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
return ConnectionPage{pagination.LinkedPageBase{PageResult: r}}
})
}

// Get retrieves a specific connection based on its unique ID.
func Get(c *fic.ServiceClient, id string) (r GetResult) {
_, r.Err = c.Get(getURL(c, id), &r.Body, nil)
return
}

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

// Primary represents primary port for connection.
type Primary struct {
PortID string `json:"portId" required:"true"`
VLAN int `json:"vlan" required:"true"`
}

// Secondary represents secondary port for connection.
type Secondary struct {
PortID string `json:"portId" required:"true"`
VLAN int `json:"vlan" required:"true"`
}

// Destination represents destination parameter for connection.
type Destination struct {
Interconnect string `json:"interconnect" required:"true"`
ServiceKey string `json:"serviceKey" required:"true"`
SharedKey string `json:"sharedKey"`
QosType string `json:"qosType" required:"true"`
AdvertisedPublicPrefixes []string `json:"advertisedPublicPrefixes" required:"true"`
RoutingRegistryName string `json:"routingRegistryName"`
}

// Source represents source parameter for connection.
type Source struct {
Primary Primary `json:"primary" required:"true"`
Secondary Secondary `json:"secondary" required:"true"`
ASN string `json:"asn" required:"true"`
}

// CreateOpts represents options used to create a connection.
type CreateOpts struct {
Name string `json:"name" required:"true"`
Source Source `json:"source" required:"true"`
Destination Destination `json:"destination" required:"true"`
Bandwidth string `json:"bandwidth" required:"true"`
PrimaryConnectedNetworkAddress string `json:"primaryConnectedNwAddress" required:"true"`
SecondaryConnectedNetworkAddress string `json:"secondaryConnectedNwAddress" required:"true"`
}

// ToConnectionCreateMap builds a request body from CreateOpts.
func (opts CreateOpts) ToConnectionCreateMap() (map[string]interface{}, error) {
return fic.BuildRequestBody(opts, "connection")
}

// Create accepts a CreateOpts struct and creates a connection
// using the values provided.
// This operation does not actually require a request body, i.e. the
// CreateOpts struct argument can be empty.
func Create(c *fic.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToConnectionCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = c.Post(createURL(c), b, &r.Body, &fic.RequestOpts{
OkCodes: []int{202},
})
return
}

// Delete accepts a unique ID and deletes the connection associated with it.
func Delete(c *fic.ServiceClient, connectionID string) (r DeleteResult) {
_, r.Err = c.Delete(deleteURL(c, connectionID), nil)
return
}

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

// DestinationForUpdate represents Destination parameter in case of Updating.
type DestinationForUpdate struct {
AdvertisedPublicPrefixes []string `json:"advertisedPublicPrefixes" required:"true"`
RoutingRegistryName string `json:"routingRegistryName"`
}

// UpdateOpts represents options used to update a connection.
type UpdateOpts struct {
Destination DestinationForUpdate `json:"destination" required:"true"`
}

// ToUpdateMap builds a request body from UpdateOpts.
func (opts UpdateOpts) ToUpdateMap() (map[string]interface{}, error) {
return fic.BuildRequestBody(opts, "connection")
}

// Update accepts a UpdateOpts struct and update a connection
// using the values provided.
func Update(c *fic.ServiceClient, connectionID string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.ToUpdateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = c.Patch(updateURL(c, connectionID), b, &r.Body, &fic.RequestOpts{
OkCodes: []int{202},
})
return
}
104 changes: 104 additions & 0 deletions fic/eri/v1/port_to_azure_microsoft_connections/results.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package port_to_azure_microsoft_connections

import (
fic "github.com/nttcom/go-fic"
"github.com/nttcom/go-fic/pagination"
)

type commonResult struct {
fic.Result
}

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

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

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

// GetResult represents the result of a get operation. Call its Extract
// method to interpret it as a Connection.
type GetResult 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 {
fic.ErrResult
}

// UpdateResult represents the result of a update operation.
type UpdateResult struct {
commonResult
}

// Connection represents connection resource.
type Connection struct {
ID string `json:"id"`
TenantID string `json:"tenantId"`
OperationStatus string `json:"operationStatus"`
Redundant bool `json:"redundant"`
Name string `json:"name"`
Bandwidth string `json:"bandwidth"`
Source Source `json:"source"`
Destination Destination `json:"destination"`
PrimaryConnectedNetworkAddress string `json:"primaryConnectedNwAddress"`
SecondaryConnectedNetworkAddress string `json:"secondaryConnectedNwAddress"`
OperationID string `json:"operationId"`
Area string `json:"area"`
}

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

// NextPageURL is invoked when a paginated collection of connections
// have 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 ConnectionPage) NextPageURL() (string, error) {
var s struct {
Links []fic.Link `json:"connections_links"`
}
err := r.ExtractInto(&s)
if err != nil {
return "", err
}
return fic.ExtractNextURL(s.Links)
}

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

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

// ExtractConnectionsInto interprets the results of a single page from a List() call,
// producing a slice of Connection entities.
func ExtractConnectionsInto(r pagination.Page, v interface{}) error {
return r.(ConnectionPage).Result.ExtractIntoSlicePtr(v, "connections")
}
2 changes: 2 additions & 0 deletions fic/eri/v1/port_to_azure_microsoft_connections/testing/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package testing contains connections unit tests
package testing
Loading

0 comments on commit f7f527f

Please sign in to comment.