github.com/aiven/aiven-go-client@v1.36.0/gcp_privatelink.go (about) 1 package aiven 2 3 import "fmt" 4 5 type ( 6 // GCPPrivatelinkHandler is the client that interacts with the Aiven GCP Privatelink API. 7 GCPPrivatelinkHandler struct { 8 client *Client 9 } 10 11 // GCPPrivatelinkResponse is a response with a GCP Privatelink details. 12 GCPPrivatelinkResponse struct { 13 APIResponse 14 State string `json:"state"` 15 GoogleServiceAttachment string `json:"google_service_attachment"` 16 } 17 18 // GCPPrivatelinkConnectionsResponse is a response with a list of GCP Privatelink connections. 19 GCPPrivatelinkConnectionsResponse struct { 20 APIResponse 21 Connections []GCPPrivatelinkConnectionResponse 22 } 23 24 // GCPPrivatelinkConnectionResponse is a response with a GCP Privatelink connection details. 25 GCPPrivatelinkConnectionResponse struct { 26 APIResponse 27 PrivatelinkConnectionID string `json:"privatelink_connection_id"` 28 State string `json:"state"` 29 UserIPAddress string `json:"user_ip_address"` 30 PSCConnectionID string `json:"psc_connection_id"` 31 } 32 33 // GCPPrivatelinkConnectionApproveRequest holds the request parameters for approving a GCP Privatelink connection. 34 GCPPrivatelinkConnectionApproveRequest struct { 35 UserIPAddress string `json:"user_ip_address"` 36 } 37 ) 38 39 // Create creates a GCP Privatelink. 40 func (h *GCPPrivatelinkHandler) Create(project, serviceName string) (*GCPPrivatelinkResponse, error) { 41 path := buildPath("project", project, "service", serviceName, "privatelink", "google") 42 43 // TODO: Remove struct{}{} when API is fixed, and use nil instead. See below for more details. 44 // 45 // Currently this endpoint requires a body, even though it's not used to process the request. 46 // We can't use nil because it's not a valid JSON, and the API returns a 400, so we use an empty struct. 47 // When the API is fixed, we can remove this workaround and use nil. 48 bts, err := h.client.doPostRequest(path, struct{}{}) 49 if err != nil { 50 return nil, err 51 } 52 53 var rsp GCPPrivatelinkResponse 54 return &rsp, checkAPIResponse(bts, &rsp) 55 } 56 57 // Update updates a GCP Privatelink. 58 func (h *GCPPrivatelinkHandler) Update(project, serviceName string) (*GCPPrivatelinkResponse, error) { 59 path := buildPath("project", project, "service", serviceName, "privatelink", "google") 60 61 bts, err := h.client.doPutRequest(path, nil) 62 if err != nil { 63 return nil, err 64 } 65 66 var rsp GCPPrivatelinkResponse 67 return &rsp, checkAPIResponse(bts, &rsp) 68 } 69 70 // Get retrieves a GCP Privatelink. 71 func (h *GCPPrivatelinkHandler) Get(project, serviceName string) (*GCPPrivatelinkResponse, error) { 72 path := buildPath("project", project, "service", serviceName, "privatelink", "google") 73 74 bts, err := h.client.doGetRequest(path, nil) 75 if err != nil { 76 return nil, err 77 } 78 79 var rsp GCPPrivatelinkResponse 80 return &rsp, checkAPIResponse(bts, &rsp) 81 } 82 83 // Delete deletes a GCP Privatelink. 84 func (h *GCPPrivatelinkHandler) Delete(project, serviceName string) error { 85 path := buildPath("project", project, "service", serviceName, "privatelink", "google") 86 87 rsp, err := h.client.doDeleteRequest(path, nil) 88 if err != nil { 89 return err 90 } 91 92 return checkAPIResponse(rsp, nil) 93 } 94 95 // Refresh refreshes a GCP Privatelink. 96 func (h *GCPPrivatelinkHandler) Refresh(project, serviceName string) error { 97 path := buildPath("project", project, "service", serviceName, "privatelink", "google", "refresh") 98 99 rsp, err := h.client.doPostRequest(path, nil) 100 if err != nil { 101 return err 102 } 103 104 return checkAPIResponse(rsp, nil) 105 } 106 107 // ConnectionApprove approves a GCP Privatelink connection. 108 func (h *GCPPrivatelinkHandler) ConnectionsList( 109 project, 110 serviceName string, 111 ) (*GCPPrivatelinkConnectionsResponse, error) { 112 path := buildPath("project", project, "service", serviceName, "privatelink", "google", "connections") 113 114 bts, err := h.client.doGetRequest(path, nil) 115 if err != nil { 116 return nil, err 117 } 118 119 var rsp GCPPrivatelinkConnectionsResponse 120 return &rsp, checkAPIResponse(bts, &rsp) 121 } 122 123 // ConnectionGet retrieves a GCP Privatelink connection. 124 // This is a convenience function that fetches all connections and filters by ID because the API does not 125 // support fetching by ID. It fetches all connections and filters by ID and returns a fake 404 if nothing is found. 126 func (h *GCPPrivatelinkHandler) ConnectionGet( 127 project, 128 serviceName string, 129 connID *string, 130 ) (*GCPPrivatelinkConnectionResponse, error) { 131 conns, err := h.ConnectionsList(project, serviceName) 132 if err != nil { 133 return nil, err 134 } 135 136 var conn GCPPrivatelinkConnectionResponse 137 138 assertedConnID := PointerToString(connID) 139 if assertedConnID == "" { 140 assertedConnID = "0" 141 } else { 142 for _, it := range conns.Connections { 143 if it.PrivatelinkConnectionID == assertedConnID { 144 conn = it 145 break 146 } 147 } 148 } 149 150 if conn.PrivatelinkConnectionID == "" { 151 return nil, Error{ 152 Message: fmt.Sprintf("GCP Privatelink connection with the ID %s does not exists", assertedConnID), 153 Status: 404, 154 } 155 } 156 157 return &conn, nil 158 } 159 160 // ConnectionApprove approves a GCP Privatelink connection. 161 func (h *GCPPrivatelinkHandler) ConnectionApprove( 162 project, 163 serviceName, 164 connID string, 165 req GCPPrivatelinkConnectionApproveRequest, 166 ) error { 167 path := buildPath( 168 "project", project, "service", serviceName, "privatelink", 169 "google", "connections", connID, "approve", 170 ) 171 172 rsp, err := h.client.doPostRequest(path, req) 173 if err != nil { 174 return err 175 } 176 177 return checkAPIResponse(rsp, nil) 178 }