github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/integration/helpers/fakeservicebroker/configuration.go (about)

     1  package fakeservicebroker
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io"
     8  	"io/ioutil"
     9  	"net/http"
    10  
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  func (f *FakeServiceBroker) configure() {
    15  	Eventually(f.alreadyPushedApp).Should(
    16  		Equal(true),
    17  		fmt.Sprintf("Expected app to be pushed and eventually available on %s", f.URL()),
    18  	)
    19  
    20  	req, err := http.NewRequest("POST", f.URL("/config"), f.configJSON())
    21  	Expect(err).ToNot(HaveOccurred())
    22  	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
    23  
    24  	resp, err := http.DefaultClient.Do(req)
    25  	Expect(err).ToNot(HaveOccurred())
    26  	body, err := ioutil.ReadAll(resp.Body)
    27  	Expect(err).ToNot(HaveOccurred())
    28  	Expect(resp.StatusCode).To(
    29  		Equal(http.StatusOK),
    30  		fmt.Sprintf("Expected POST /config to succeed. Response body: '%s'", string(body)),
    31  	)
    32  	defer resp.Body.Close()
    33  }
    34  
    35  func (f *FakeServiceBroker) configJSON() io.Reader {
    36  	f.behaviors.Catalog = syncResponse().
    37  		withBody(map[string]interface{}{
    38  			"services": f.Services,
    39  		}).
    40  		withStatus(f.catalogStatus)
    41  
    42  	config := configuration{Behaviors: f.behaviors}
    43  
    44  	data, err := json.Marshal(config)
    45  	Expect(err).NotTo(HaveOccurred())
    46  	return bytes.NewReader(data)
    47  }
    48  
    49  func asyncResponse() responseMock {
    50  	return responseMock{
    51  		SleepSeconds: 0,
    52  		Status:       http.StatusAccepted,
    53  		Body:         map[string]interface{}{},
    54  	}
    55  }
    56  
    57  func syncResponse() responseMock {
    58  	return responseMock{
    59  		SleepSeconds: 0,
    60  		Status:       http.StatusOK,
    61  		Body:         map[string]interface{}{},
    62  	}
    63  }
    64  
    65  func (r responseMock) asyncOnly() responseMock {
    66  	var nillableTrue = new(bool)
    67  	*nillableTrue = true
    68  	r.AsyncOnly = nillableTrue
    69  	return r
    70  }
    71  
    72  func (r responseMock) withBody(body map[string]interface{}) responseMock {
    73  	r.Body = body
    74  	return r
    75  }
    76  
    77  func (r responseMock) withStatus(status int) responseMock {
    78  	r.Status = status
    79  	return r
    80  }
    81  
    82  type configuration struct {
    83  	Behaviors behaviors `json:"behaviors"`
    84  }
    85  
    86  type behaviors struct {
    87  	Catalog             responseMock                       `json:"catalog"`
    88  	Provision           map[string]responseMock            `json:"provision"`
    89  	Bind                map[string]responseMock            `json:"bind"`
    90  	Fetch               map[string]map[string]responseMock `json:"fetch"`
    91  	Update              map[string]responseMock            `json:"update"`
    92  	Deprovision         map[string]responseMock            `json:"deprovision"`
    93  	Unbind              map[string]responseMock            `json:"unbind"`
    94  	FetchServiceBinding map[string]responseMock            `json:"fetch_service_binding"`
    95  }
    96  
    97  type service struct {
    98  	Name                 string   `json:"name"`
    99  	ID                   string   `json:"id"`
   100  	Description          string   `json:"description"`
   101  	Bindable             bool     `json:"bindable"`
   102  	InstancesRetrievable bool     `json:"instances_retrievable"`
   103  	BindingsRetrievable  bool     `json:"bindings_retrievable"`
   104  	Plans                []plan   `json:"plans"`
   105  	Requires             []string `json:"requires"`
   106  	Metadata             metadata `json:"metadata"`
   107  }
   108  
   109  func (s service) PlanNames() []string {
   110  	var planNames []string
   111  	for _, plan := range s.Plans {
   112  		planNames = append(planNames, plan.Name)
   113  	}
   114  	return planNames
   115  }
   116  
   117  type plan struct {
   118  	Name            string           `json:"name"`
   119  	ID              string           `json:"id"`
   120  	Description     string           `json:"description"`
   121  	MaintenanceInfo *maintenanceInfo `json:"maintenance_info,omitempty"`
   122  }
   123  
   124  func (p *plan) SetMaintenanceInfo(version, description string) {
   125  	p.MaintenanceInfo.Version = version
   126  	p.MaintenanceInfo.Description = description
   127  }
   128  
   129  func (p *plan) RemoveMaintenanceInfo() {
   130  	p.MaintenanceInfo = nil
   131  }
   132  
   133  type maintenanceInfo struct {
   134  	Version     string `json:"version"`
   135  	Description string `json:"description"`
   136  }
   137  
   138  type responseMock struct {
   139  	SleepSeconds int                    `json:"sleep_seconds"`
   140  	Status       int                    `json:"status"`
   141  	Body         map[string]interface{} `json:"body"`
   142  	AsyncOnly    *bool                  `json:"async_only,omitempty"`
   143  }
   144  
   145  type metadata struct {
   146  	Shareable        bool   `json:"shareable"`
   147  	DocumentationURL string `json:"documentationUrl"`
   148  }