Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable wait on GCP Peering status #38

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
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: 29 additions & 18 deletions api/vpc_gcp_peering.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,40 @@ func (api *API) waitForGcpPeeringStatus(instanceID int, peerID string) error {
}
}

func (api *API) RequestVpcGcpPeering(instanceID int, params map[string]interface{}) (map[string]interface{}, error) {
data := make(map[string]interface{})
failed := make(map[string]interface{})
func (api *API) RequestVpcGcpPeering(instanceID int, params map[string]interface{},
waitOnStatus bool) (map[string]interface{}, error) {
var (
data map[string]interface{}
failed map[string]interface{}
path = fmt.Sprintf("api/instances/%v/vpc-peering", instanceID)
)

log.Printf("[DEBUG] go-api::vpc_gcp_peering::request params: %v", params)
path := fmt.Sprintf("api/instances/%v/vpc-peering", instanceID)
response, err := api.sling.New().Post(path).BodyJSON(params).Receive(&data, &failed)

if err != nil {
return nil, err
}
if response.StatusCode != 200 {
return nil, fmt.Errorf("request VPC peering failed, status: %v, message: %s", response.StatusCode, failed)
}

log.Printf("[DEBUG] go-api::vpc_gcp_peering::request waiting for active state")
api.waitForGcpPeeringStatus(instanceID, data["peering"].(string))
if waitOnStatus {
log.Printf("[DEBUG] go-api::vpc_gcp_peering::request waiting for active state")
api.waitForGcpPeeringStatus(instanceID, data["peering"].(string))
}
return data, nil
}

func (api *API) ReadVpcGcpPeering(instanceID int, peerID string) (map[string]interface{}, error) {
data := make(map[string]interface{})
failed := make(map[string]interface{})
var (
data map[string]interface{}
failed map[string]interface{}
path = fmt.Sprintf("/api/instances/%v/vpc-peering", instanceID)
)

log.Printf("[DEBUG] go-api::vpc_gcp_peering::read instance_id: %v, peer_id: %v", instanceID, peerID)
path := fmt.Sprintf("/api/instances/%v/vpc-peering", instanceID)
response, err := api.sling.New().Get(path).Receive(&data, &failed)
log.Printf("[DEBUG] go-api::vpc_gcp_peering::read data: %v", data)

if err != nil {
return nil, err
}
Expand All @@ -71,11 +78,13 @@ func (api *API) UpdateVpcGcpPeering(instanceID int, peerID string) (map[string]i
}

func (api *API) RemoveVpcGcpPeering(instanceID int, peerID string) error {
failed := make(map[string]interface{})
var (
failed map[string]interface{}
path = fmt.Sprintf("/api/instances/%v/vpc-peering/%v", instanceID, peerID)
)

log.Printf("[DEBUG] go-api::vpc_gcp_peering::remove instance id: %v, peering id: %v", instanceID, peerID)
path := fmt.Sprintf("/api/instances/%v/vpc-peering/%v", instanceID, peerID)
response, err := api.sling.New().Delete(path).Receive(nil, &failed)

if err != nil {
return err
}
Expand All @@ -91,13 +100,15 @@ func (api *API) ReadVpcGcpInfo(instanceID int) (map[string]interface{}, error) {
}

func (api *API) readVpcGcpInfoWithRetry(instanceID, attempts, sleep int) (map[string]interface{}, error) {
data := make(map[string]interface{})
failed := make(map[string]interface{})
var (
data map[string]interface{}
failed map[string]interface{}
path = fmt.Sprintf("/api/instances/%v/vpc-peering/info", instanceID)
)

log.Printf("[DEBUG] go-api::vpc_gcp_peering::info instance id: %v", instanceID)
path := fmt.Sprintf("/api/instances/%v/vpc-peering/info", instanceID)
response, err := api.sling.New().Get(path).Receive(&data, &failed)
log.Printf("[DEBUG] go-api::vpc_gcp_peering::info data: %v", data)

if err != nil {
return nil, err
}
Expand Down
47 changes: 29 additions & 18 deletions api/vpc_gcp_peering_withvpcid.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,40 @@ func (api *API) waitForGcpPeeringStatusWithVpcId(vpcID, peerID string) error {
}
}

func (api *API) RequestVpcGcpPeeringWithVpcId(vpcID string, params map[string]interface{}) (map[string]interface{}, error) {
data := make(map[string]interface{})
failed := make(map[string]interface{})
func (api *API) RequestVpcGcpPeeringWithVpcId(vpcID string, params map[string]interface{},
waitOnStatus bool) (map[string]interface{}, error) {
var (
data map[string]interface{}
failed map[string]interface{}
path = fmt.Sprintf("api/vpcs/%s/vpc-peering", vpcID)
)

log.Printf("[DEBUG] go-api::vpc_gcp_peering_withvpcid::request params: %v", params)
path := fmt.Sprintf("api/vpcs/%s/vpc-peering", vpcID)
response, err := api.sling.New().Post(path).BodyJSON(params).Receive(&data, &failed)

if err != nil {
return nil, err
}
if response.StatusCode != 200 {
return nil, fmt.Errorf("request VPC peering failed, status: %v, message: %s", response.StatusCode, failed)
}

log.Printf("[DEBUG] go-api::vpc_gcp_peering_withvpcid::request waiting for active state")
api.waitForGcpPeeringStatusWithVpcId(vpcID, data["peering"].(string))
if waitOnStatus {
log.Printf("[DEBUG] go-api::vpc_gcp_peering_withvpcid::request waiting for active state")
api.waitForGcpPeeringStatusWithVpcId(vpcID, data["peering"].(string))
}
return data, nil
}

func (api *API) ReadVpcGcpPeeringWithVpcId(vpcID, peerID string) (map[string]interface{}, error) {
data := make(map[string]interface{})
failed := make(map[string]interface{})
var (
data map[string]interface{}
failed map[string]interface{}
path = fmt.Sprintf("/api/vpcs/%s/vpc-peering", vpcID)
)

log.Printf("[DEBUG] go-api::vpc_gcp_peering_withvpcid::read instance_id: %s, peer_id: %s", vpcID, peerID)
path := fmt.Sprintf("/api/vpcs/%s/vpc-peering", vpcID)
response, err := api.sling.New().Get(path).Receive(&data, &failed)
log.Printf("[DEBUG] go-api::vpc_gcp_peering_withvpcid::read data: %v", data)

if err != nil {
return nil, err
}
Expand All @@ -73,11 +80,13 @@ func (api *API) UpdateVpcGcpPeeringWithVpcId(vpcID, peerID string) (map[string]i
}

func (api *API) RemoveVpcGcpPeeringWithVpcId(vpcID, peerID string) error {
failed := make(map[string]interface{})
var (
failed map[string]interface{}
path = fmt.Sprintf("/api/vpcs/%s/vpc-peering/%s", vpcID, peerID)
)

log.Printf("[DEBUG] go-api::vpc_gcp_peering_withvpcid::remove vpc id: %s, peering id: %s", vpcID, peerID)
path := fmt.Sprintf("/api/vpcs/%s/vpc-peering/%s", vpcID, peerID)
response, err := api.sling.New().Delete(path).Receive(nil, &failed)

if err != nil {
return err
}
Expand All @@ -93,13 +102,15 @@ func (api *API) ReadVpcGcpInfoWithVpcId(vpcID string) (map[string]interface{}, e
}

func (api *API) readVpcGcpInfoWithRetryWithVpcId(vpcID string, attempts, sleep int) (map[string]interface{}, error) {
data := make(map[string]interface{})
failed := make(map[string]interface{})
var (
data map[string]interface{}
failed map[string]interface{}
path = fmt.Sprintf("/api/vpcs/%s/vpc-peering/info", vpcID)
)

log.Printf("[DEBUG] go-api::vpc_gcp_peering_withvpcid::info vpc id: %s", vpcID)
path := fmt.Sprintf("/api/vpcs/%s/vpc-peering/info", vpcID)
response, err := api.sling.New().Get(path).Receive(&data, &failed)
log.Printf("[DEBUG] go-api::vpc_gcp_peering_withvpcid::info data: %v", data)

if err != nil {
return nil, err
}
Expand Down