github.com/loafoe/cli@v7.1.0+incompatible/integration/helpers/service_instance.go (about)

     1  package helpers
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"strings"
     7  
     8  	. "github.com/onsi/gomega"
     9  	. "github.com/onsi/gomega/gexec"
    10  )
    11  
    12  // ServiceInstanceGUID represents a service instance relationship
    13  type ServiceInstanceGUID struct {
    14  	Resources []struct {
    15  		Metadata struct {
    16  			GUID string `json:"guid"`
    17  		} `json:"metadata"`
    18  	} `json:"resources"`
    19  }
    20  
    21  // ManagedServiceInstanceGUID returns the GUID for a managed service instance.
    22  func ManagedServiceInstanceGUID(managedServiceInstanceName string) string {
    23  	session := CF("curl", fmt.Sprintf("/v2/service_instances?q=name:%s", managedServiceInstanceName))
    24  	Eventually(session).Should(Exit(0))
    25  
    26  	rawJSON := strings.TrimSpace(string(session.Out.Contents()))
    27  
    28  	var serviceInstanceGUID ServiceInstanceGUID
    29  	err := json.Unmarshal([]byte(rawJSON), &serviceInstanceGUID)
    30  	Expect(err).NotTo(HaveOccurred())
    31  
    32  	Expect(serviceInstanceGUID.Resources).To(HaveLen(1))
    33  	return serviceInstanceGUID.Resources[0].Metadata.GUID
    34  }
    35  
    36  // UserProvidedServiceInstanceGUID returns the GUID for a user provided service instance.
    37  func UserProvidedServiceInstanceGUID(userProvidedServiceInstanceName string) string {
    38  	session := CF("curl", fmt.Sprintf("/v2/user_provided_service_instances?q=name:%s", userProvidedServiceInstanceName))
    39  	Eventually(session).Should(Exit(0))
    40  
    41  	rawJSON := strings.TrimSpace(string(session.Out.Contents()))
    42  
    43  	var serviceInstanceGUID ServiceInstanceGUID
    44  	err := json.Unmarshal([]byte(rawJSON), &serviceInstanceGUID)
    45  	Expect(err).NotTo(HaveOccurred())
    46  
    47  	Expect(serviceInstanceGUID.Resources).To(HaveLen(1))
    48  	return serviceInstanceGUID.Resources[0].Metadata.GUID
    49  }