github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/icd/icdv4/connection.go (about)

     1  package icdv4
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/IBM-Cloud/bluemix-go/client"
     7  	"github.com/IBM-Cloud/bluemix-go/utils"
     8  )
     9  
    10  type ConnectionReq struct {
    11  	Password        string `json:"password,omitempty"`
    12  	CertificateRoot string `json:"certificate_root,omitempty"`
    13  }
    14  
    15  type ConnectionRes struct {
    16  	Connection Connection `json:"connection"`
    17  }
    18  
    19  type Connection struct {
    20  	Rediss   Uri          `json:"rediss"`
    21  	Grpc     Uri          `json:"grpc"`
    22  	Postgres Uri          `json:"postgres"`
    23  	Https    Uri          `json:"https"`
    24  	Amqps    Uri          `json:"amqps"`
    25  	Cli      CliConn      `json:"cli"`
    26  	Mongo    Uri          `json:"mongodb"`
    27  	Secure   CassandraUri `json:"secure"`
    28  	Mysql    Uri          `json:"mysql"`
    29  }
    30  
    31  type CassandraUri struct {
    32  	Hosts []struct {
    33  		HostName string `json:"hostname"`
    34  		Port     int    `json:"port"`
    35  	} `json:"hosts"`
    36  	Authentication struct {
    37  		Method   string `json:"method"`
    38  		UserName string `json:"username"`
    39  		Password string `json:"password"`
    40  	}
    41  	Bundle struct {
    42  		Name         string `json:"name"`
    43  		BundleBase64 string `json:"bundle_base64"`
    44  	} `json:"bundle"`
    45  }
    46  
    47  type Uri struct {
    48  	Type     string   `json:"type"`
    49  	Composed []string `json:"composed"`
    50  	Scheme   string   `json:"scheme"`
    51  	Hosts    []struct {
    52  		HostName string `json:"hostname"`
    53  		Port     int    `json:"port"`
    54  	} `json:"hosts"`
    55  	Path           string      `json:"path"`
    56  	QueryOptions   interface{} `json:"query_options"`
    57  	Authentication struct {
    58  		Method   string `json:"method"`
    59  		UserName string `json:"username"`
    60  		Password string `json:"password"`
    61  	}
    62  	Certificate struct {
    63  		Name              string `json:"name"`
    64  		CertificateBase64 string `json:"certificate_base64"`
    65  	} `json:"certificate"`
    66  	Database interface{} `json:"database"`
    67  }
    68  
    69  type CliConn struct {
    70  	Type        string      `json:"type"`
    71  	Composed    []string    `json:"composed"`
    72  	Environment interface{} `json:"environment"`
    73  	Bin         string      `json:"bin"`
    74  	Arguments   [][]string  `json:"arguments"`
    75  	Certificate struct {
    76  		Name              string `json:"name"`
    77  		CertificateBase64 string `json:"certificate_base64"`
    78  	} `json:"certificate"`
    79  }
    80  
    81  type Connections interface {
    82  	GetConnection(icdId string, userId string, endpoint ...string) (Connection, error)
    83  	GetConnectionSubstitution(icdId string, userID string, connectionReq ConnectionReq) (Connection, error)
    84  }
    85  
    86  type connections struct {
    87  	client *client.Client
    88  }
    89  
    90  func newConnectionAPI(c *client.Client) Connections {
    91  	return &connections{
    92  		client: c,
    93  	}
    94  }
    95  
    96  func (r *connections) GetConnection(icdId string, userId string, endpoint ...string) (Connection, error) {
    97  	connectionRes := ConnectionRes{}
    98  	connectionEndpoint := "public"
    99  	if len(endpoint) > 0 {
   100  		connectionEndpoint = endpoint[0]
   101  	}
   102  	rawURL := fmt.Sprintf("/v4/ibm/deployments/%s/users/%s/connections/%s", utils.EscapeUrlParm(icdId), userId, connectionEndpoint)
   103  	_, err := r.client.Get(rawURL, &connectionRes)
   104  	if err != nil {
   105  		return connectionRes.Connection, err
   106  	}
   107  	return connectionRes.Connection, nil
   108  }
   109  
   110  func (r *connections) GetConnectionSubstitution(icdId string, userID string, connectionReq ConnectionReq) (Connection, error) {
   111  	connectionResSub := ConnectionRes{}
   112  	rawURL := fmt.Sprintf("/v4/ibm/deployments/%s/connections", utils.EscapeUrlParm(icdId))
   113  	_, err := r.client.Post(rawURL, &connectionReq, &connectionResSub)
   114  	if err != nil {
   115  		return connectionResSub.Connection, err
   116  	}
   117  	return connectionResSub.Connection, nil
   118  }