func CreateEndpoint()

in internal/client/connections/endpoints.go [48:110]


func CreateEndpoint(name string, serviceAttachment string, description string, wait bool) (respBody []byte, err error) {
	endpointStr := []string{}

	endpointStr = append(endpointStr, "\"name\":\""+
		fmt.Sprintf("projects/%s/locations/%s/endpointAttachments/%s",
			apiclient.GetProjectID(), apiclient.GetRegion(), name)+"\"")
	endpointStr = append(endpointStr, "\"serviceAttachment\":\""+serviceAttachment+"\"")

	if description != "" {
		endpointStr = append(endpointStr, "\"description\":\""+description+"\"")
	}

	payload := "{" + strings.Join(endpointStr, ",") + "}"

	u, _ := url.Parse(apiclient.GetBaseConnectorEndpointAttachURL())
	u.Path = path.Join(u.Path)

	q := u.Query()
	q.Set("endpointAttachmentId", name)
	u.RawQuery = q.Encode()

	respBody, err = apiclient.HttpClient(u.String(), payload)

	if wait {
		apiclient.ClientPrintHttpResponse.Set(false)
		defer apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting())

		o := operation{}
		if err = json.Unmarshal(respBody, &o); err != nil {
			return nil, err
		}

		operationId := filepath.Base(o.Name)
		clilog.Info.Printf("Checking connection status for %s in %d seconds\n", operationId, interval)

		stop := apiclient.Every(interval*time.Second, func(time.Time) bool {
			var respBody []byte

			if respBody, err = GetOperation(operationId); err != nil {
				return false
			}

			if err = json.Unmarshal(respBody, &o); err != nil {
				return false
			}

			if o.Done {
				if o.Error != nil {
					clilog.Error.Printf("Connection completed with error: %s\n", o.Error.Message)
				} else {
					clilog.Info.Println("Connection completed successfully!")
				}
				return false
			} else {
				clilog.Info.Printf("Connection status is: %t. Waiting %d seconds.\n", o.Done, interval)
				return true
			}
		})

		<-stop
	}
	return
}