github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/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  }
    33  
    34  func newVPCsAPI(c *client.Client) VPCs {
    35  	return &vpc{
    36  		client: c,
    37  	}
    38  }
    39  
    40  // ListVPCs lists the vpcs
    41  func (r *vpc) ListVPCs(target ClusterTargetHeader) ([]VPCConfig, error) {
    42  	var successV []VPCConfig
    43  	_, err := r.client.Get(fmt.Sprintf("/v2/vpc/getVPCs?provider=%s", target.Provider), &successV, target.ToMap())
    44  	return successV, err
    45  }
    46  
    47  type OutboundTrafficProtectionRequest struct {
    48  	Cluster   string `json:"cluster" binding:"required"`
    49  	Operation string `json:"operation" binding:"required"`
    50  }
    51  
    52  // Set Outbound traffic protection
    53  func (v *vpc) SetOutboundTrafficProtection(clusterID string, enable bool, target ClusterTargetHeader) error {
    54  	request := OutboundTrafficProtectionRequest{
    55  		Cluster:   clusterID,
    56  		Operation: DisableOutboundProtection,
    57  	}
    58  	if enable {
    59  		request.Operation = EnableOutboundProtection
    60  	}
    61  
    62  	_, err := v.client.Post("/network/v2/outbound-traffic-protection", request, nil, target.ToMap())
    63  
    64  	return err
    65  }