Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 35 additions & 12 deletions api/privatelink.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package api
import (
"fmt"
"log"
"strings"
"time"
)

// EnablePrivatelink: Enable PrivateLink and wait until finished.
// Need to enable VPC for an instance, if no standalone VPC used.
// Wait until finished with configureable sleep and timeout.
func (api *API) EnablePrivatelink(instanceID int, params map[string][]interface{}, sleep, timeout int) error {
func (api *API) EnablePrivatelink(instanceID int, params map[string][]interface{},
sleep, timeout int) error {

var (
failed map[string]interface{}
path = fmt.Sprintf("/api/instances/%d/privatelink", instanceID)
Expand All @@ -24,16 +27,23 @@ func (api *API) EnablePrivatelink(instanceID int, params map[string][]interface{
return err
}

if response.StatusCode == 204 {
switch response.StatusCode {
case 204:
return api.waitForEnablePrivatelinkWithRetry(instanceID, 1, sleep, timeout)
} else {
default:
return fmt.Errorf("enable PrivateLink failed, status: %v, message: %s",
response.StatusCode, failed)
}
}

// ReadPrivatelink: Reads PrivateLink information
func (api *API) ReadPrivatelink(instanceID int) (map[string]interface{}, error) {
func (api *API) ReadPrivatelink(instanceID, sleep, timeout int) (map[string]interface{}, error) {
return api.readPrivateLinkWithRetry(instanceID, 1, sleep, timeout)
}

func (api *API) readPrivateLinkWithRetry(instanceID, attempt, sleep, timeout int) (
map[string]interface{}, error) {

var (
data map[string]interface{}
failed map[string]interface{}
Expand All @@ -43,14 +53,25 @@ func (api *API) ReadPrivatelink(instanceID int) (map[string]interface{}, error)
response, err := api.sling.New().Get(path).Receive(&data, &failed)
if err != nil {
return nil, err
} else if attempt*sleep > timeout {
return nil, fmt.Errorf("read PrivateLink failed, reached timeout of %d seconds", timeout)
}

if response.StatusCode == 200 {
switch response.StatusCode {
case 200:
return data, nil
} else {
return nil, fmt.Errorf("read PrivateLink failed, status: %v, message: %s",
response.StatusCode, failed)
case 400:
if strings.Compare(failed["error"].(string), "Timeout talking to backend") == 0 {
log.Printf("[INFO] go-api::privatelink::read Timeout talking to backend "+
"attempt: %d, until timeout: %d", attempt, (timeout - (attempt * sleep)))
attempt++
time.Sleep(time.Duration(sleep) * time.Second)
return api.readPrivateLinkWithRetry(instanceID, attempt, sleep, timeout)
}
}

return nil, fmt.Errorf("read PrivateLink failed, status: %v, message: %s",
response.StatusCode, failed)
}

// UpdatePrivatelink: Update allowed principals or subscriptions
Expand All @@ -65,9 +86,10 @@ func (api *API) UpdatePrivatelink(instanceID int, params map[string][]interface{
return err
}

if response.StatusCode == 204 {
switch response.StatusCode {
case 204:
return nil
} else {
default:
return fmt.Errorf("update Privatelink failed, status: %v, message: %s",
response.StatusCode, failed)
}
Expand All @@ -85,9 +107,10 @@ func (api *API) DisablePrivatelink(instanceID int) error {
return err
}

if response.StatusCode == 204 {
switch response.StatusCode {
case 204:
return nil
} else {
default:
return fmt.Errorf("disable Privatelink failed, status: %v, message: %s",
response.StatusCode, failed)
}
Expand Down