2021-12-27 14:56:50 +00:00
package cfapi
2021-11-26 12:37:54 +00:00
import (
"net/url"
"strconv"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
)
var (
2021-12-27 14:56:50 +00:00
filterVnetId = cli . StringFlag {
2021-11-26 12:37:54 +00:00
Name : "id" ,
Usage : "List virtual networks with the given `ID`" ,
}
2021-12-27 14:56:50 +00:00
filterVnetByName = cli . StringFlag {
2021-11-26 12:37:54 +00:00
Name : "name" ,
Usage : "List virtual networks with the given `NAME`" ,
}
2021-12-27 14:56:50 +00:00
filterDefaultVnet = cli . BoolFlag {
2021-11-26 12:37:54 +00:00
Name : "is-default" ,
Usage : "If true, lists the virtual network that is the default one. If false, lists all non-default virtual networks for the account. If absent, all are included in the results regardless of their default status." ,
}
2021-12-27 14:56:50 +00:00
filterDeletedVnet = cli . BoolFlag {
2021-11-26 12:37:54 +00:00
Name : "show-deleted" ,
Usage : "If false (default), only show non-deleted virtual networks. If true, only show deleted virtual networks." ,
}
2021-12-27 14:56:50 +00:00
VnetFilterFlags = [ ] cli . Flag {
& filterVnetId ,
& filterVnetByName ,
& filterDefaultVnet ,
& filterDeletedVnet ,
2021-11-26 12:37:54 +00:00
}
)
2021-12-27 14:56:50 +00:00
// VnetFilter which virtual networks get queried.
type VnetFilter struct {
2021-11-26 12:37:54 +00:00
queryParams url . Values
}
2021-12-27 14:56:50 +00:00
func NewVnetFilter ( ) * VnetFilter {
return & VnetFilter {
2021-11-26 12:37:54 +00:00
queryParams : url . Values { } ,
}
}
2021-12-27 14:56:50 +00:00
func ( f * VnetFilter ) ById ( vnetId uuid . UUID ) {
2021-11-26 12:37:54 +00:00
f . queryParams . Set ( "id" , vnetId . String ( ) )
}
2021-12-27 14:56:50 +00:00
func ( f * VnetFilter ) ByName ( name string ) {
2021-11-26 12:37:54 +00:00
f . queryParams . Set ( "name" , name )
}
2021-12-27 14:56:50 +00:00
func ( f * VnetFilter ) ByDefaultStatus ( isDefault bool ) {
2021-11-26 12:37:54 +00:00
f . queryParams . Set ( "is_default" , strconv . FormatBool ( isDefault ) )
}
2021-12-27 14:56:50 +00:00
func ( f * VnetFilter ) WithDeleted ( isDeleted bool ) {
2021-11-26 12:37:54 +00:00
f . queryParams . Set ( "is_deleted" , strconv . FormatBool ( isDeleted ) )
}
2021-12-27 14:56:50 +00:00
func ( f * VnetFilter ) MaxFetchSize ( max uint ) {
2021-11-26 12:37:54 +00:00
f . queryParams . Set ( "per_page" , strconv . Itoa ( int ( max ) ) )
}
2021-12-27 14:56:50 +00:00
func ( f VnetFilter ) Encode ( ) string {
2021-11-26 12:37:54 +00:00
return f . queryParams . Encode ( )
}
// NewFromCLI parses CLI flags to discover which filters should get applied to list virtual networks.
2021-12-27 14:56:50 +00:00
func NewFromCLI ( c * cli . Context ) ( * VnetFilter , error ) {
f := NewVnetFilter ( )
2021-11-26 12:37:54 +00:00
if id := c . String ( "id" ) ; id != "" {
vnetId , err := uuid . Parse ( id )
if err != nil {
return nil , errors . Wrapf ( err , "%s is not a valid virtual network ID" , id )
}
f . ById ( vnetId )
}
if name := c . String ( "name" ) ; name != "" {
f . ByName ( name )
}
if c . IsSet ( "is-default" ) {
f . ByDefaultStatus ( c . Bool ( "is-default" ) )
}
f . WithDeleted ( c . Bool ( "show-deleted" ) )
if maxFetch := c . Int ( "max-fetch-size" ) ; maxFetch > 0 {
f . MaxFetchSize ( uint ( maxFetch ) )
}
return f , nil
}