github.com/IBM-Cloud/bluemix-go@v0.0.0-20241117121028-a3be206688b3/api/container/containerv2/vpcs.go (about)

     1  package containerv2
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/IBM-Cloud/bluemix-go/client"
     7  )
     8  
     9  const (
    10  	// EnableOutboundProtection configures a secure by default cluster to block all public outbound traffic
    11  	EnableOutboundProtection = "enable-outbound-protection"
    12  	// DisableOutboundProtection configures a secure by default cluster to allow all public outbound traffic
    13  	DisableOutboundProtection = "disable-outbound-protection"
    14  )
    15  
    16  type VPCConfig struct {
    17  	ID            string   `json:"id"`
    18  	Name          string   `json:"name"`
    19  	Provider      string   `json:"provider"`
    20  	ResourceGroup string   `json:"resourceGroup"`
    21  	Zones         []string `json:"zones"`
    22  }
    23  
    24  type vpc struct {
    25  	client *client.Client
    26  }
    27  
    28  // VPCs interface
    29  type VPCs interface {
    30  	ListVPCs(target ClusterTargetHeader) ([]VPCConfig, error)
    31  	SetOutboundTrafficProtection(string, bool, ClusterTargetHeader) error
    32  	EnableSecureByDefault(string, bool, ClusterTargetHeader) error
    33  }
    34  
    35  func newVPCsAPI(c *client.Client) VPCs {
    36  	return &vpc{
    37  		client: c,
    38  	}
    39  }
    40  
    41  // ListVPCs lists the vpcs
    42  func (r *vpc) ListVPCs(target ClusterTargetHeader) ([]VPCConfig, error) {
    43  	var successV []VPCConfig
    44  	_, err := r.client.Get(fmt.Sprintf("/v2/vpc/getVPCs?provider=%s", target.Provider), &successV, target.ToMap())
    45  	return successV, err
    46  }
    47  
    48  type OutboundTrafficProtectionRequest struct {
    49  	Cluster   string `json:"cluster" binding:"required"`
    50  	Operation string `json:"operation" binding:"required"`
    51  }
    52  
    53  // Set Outbound traffic protection
    54  func (v *vpc) SetOutboundTrafficProtection(clusterID string, enable bool, target ClusterTargetHeader) error {
    55  	request := OutboundTrafficProtectionRequest{
    56  		Cluster:   clusterID,
    57  		Operation: DisableOutboundProtection,
    58  	}
    59  	if enable {
    60  		request.Operation = EnableOutboundProtection
    61  	}
    62  
    63  	_, err := v.client.Post("/network/v2/outbound-traffic-protection", request, nil, target.ToMap())
    64  
    65  	return err
    66  }
    67  
    68  type EnableSecureByDefaultClusterRequest struct {
    69  	Cluster                          string `json:"cluster" binding:"required"`
    70  	DisableOutboundTrafficProtection bool   `json:"disableOutboundTrafficProtection,omitempty"`
    71  }
    72  
    73  // Enable Secure by Default
    74  func (v *vpc) EnableSecureByDefault(clusterID string, enable bool, target ClusterTargetHeader) error {
    75  	request := EnableSecureByDefaultClusterRequest{
    76  		Cluster:                          clusterID,
    77  		DisableOutboundTrafficProtection: enable,
    78  	}
    79  
    80  	_, err := v.client.Post("/network/v2/secure-by-default/enable", request, nil, target.ToMap())
    81  
    82  	return err
    83  }