github.com/aiven/aiven-go-client@v1.36.0/azure_privatelink.go (about) 1 package aiven 2 3 import "fmt" 4 5 type ( 6 // AzurePrivatelinkHandler is the client that interacts with the Azure Privatelink API on Aiven. 7 AzurePrivatelinkHandler struct { 8 client *Client 9 } 10 11 // AzurePrivatelinkRequest holds the parameters to create a new 12 // or update an existing Azure Privatelink. 13 AzurePrivatelinkRequest struct { 14 UserSubscriptionIDs []string `json:"user_subscription_ids"` 15 } 16 17 // AzurePrivatelinkResponse represents the response from Aiven after 18 // interacting with the Azure Privatelink. 19 AzurePrivatelinkResponse struct { 20 APIResponse 21 AzureServiceAlias string `json:"azure_service_alias"` 22 AzureServiceID string `json:"azure_service_id"` 23 Message string `json:"message"` 24 State string `json:"state"` 25 UserSubscriptionIDs []string `json:"user_subscription_ids"` 26 } 27 28 AzurePrivatelinkConnectionUpdateRequest struct { 29 UserIPAddress string `json:"user_ip_address"` 30 } 31 32 AzurePrivatelinkConnectionsResponse struct { 33 APIResponse 34 Connections []AzurePrivatelinkConnectionResponse 35 } 36 37 AzurePrivatelinkConnectionResponse struct { 38 APIResponse 39 PrivateEndpointID string `json:"private_endpoint_id"` 40 PrivatelinkConnectionID string `json:"privatelink_connection_id"` 41 State string `json:"state"` 42 UserIPAddress string `json:"user_ip_address"` 43 } 44 ) 45 46 // Create creates an Azure Privatelink 47 func (h *AzurePrivatelinkHandler) Create(project, serviceName string, r AzurePrivatelinkRequest) (*AzurePrivatelinkResponse, error) { 48 path := buildPath("project", project, "service", serviceName, "privatelink", "azure") 49 bts, err := h.client.doPostRequest(path, r) 50 if err != nil { 51 return nil, err 52 } 53 54 var rsp AzurePrivatelinkResponse 55 if err := checkAPIResponse(bts, &rsp); err != nil { 56 return nil, err 57 } 58 59 return &rsp, nil 60 } 61 62 // Update updates an Azure Privatelink 63 func (h *AzurePrivatelinkHandler) Update(project, serviceName string, r AzurePrivatelinkRequest) (*AzurePrivatelinkResponse, error) { 64 path := buildPath("project", project, "service", serviceName, "privatelink", "azure") 65 bts, err := h.client.doPutRequest(path, r) 66 if err != nil { 67 return nil, err 68 } 69 70 var rsp AzurePrivatelinkResponse 71 if err := checkAPIResponse(bts, &rsp); err != nil { 72 return nil, err 73 } 74 75 return &rsp, nil 76 } 77 78 // Get retrieves an Azure Privatelink 79 func (h *AzurePrivatelinkHandler) Get(project, serviceName string) (*AzurePrivatelinkResponse, error) { 80 path := buildPath("project", project, "service", serviceName, "privatelink", "azure") 81 bts, err := h.client.doGetRequest(path, nil) 82 if err != nil { 83 return nil, err 84 } 85 86 var rsp AzurePrivatelinkResponse 87 if err := checkAPIResponse(bts, &rsp); err != nil { 88 return nil, err 89 } 90 91 return &rsp, nil 92 } 93 94 // Delete deletes an Azure Privatelink 95 func (h *AzurePrivatelinkHandler) Delete(project, serviceName string) error { 96 path := buildPath("project", project, "service", serviceName, "privatelink", "azure") 97 rsp, err := h.client.doDeleteRequest(path, nil) 98 if err != nil { 99 return err 100 } 101 102 return checkAPIResponse(rsp, nil) 103 } 104 105 // Refresh refreshes an Azure Privatelink 106 func (h *AzurePrivatelinkHandler) Refresh(project, serviceName string) error { 107 path := buildPath("project", project, "service", serviceName, "privatelink", "azure", "refresh") 108 rsp, err := h.client.doPostRequest(path, nil) 109 if err != nil { 110 return err 111 } 112 113 return checkAPIResponse(rsp, nil) 114 } 115 116 // ConnectionApprove approves an Azure Privatelink connection 117 func (h *AzurePrivatelinkHandler) ConnectionsList(project, serviceName string) (*AzurePrivatelinkConnectionsResponse, error) { 118 path := buildPath("project", project, "service", serviceName, "privatelink", "azure", "connections") 119 bts, err := h.client.doGetRequest(path, nil) 120 if err != nil { 121 return nil, err 122 } 123 124 var rsp AzurePrivatelinkConnectionsResponse 125 if err := checkAPIResponse(bts, &rsp); err != nil { 126 return nil, err 127 } 128 return &rsp, nil 129 } 130 131 // ConnectionGet retrieves a Azure Privatelink connection. 132 // This is a convenience function that fetches all connections and filters by ID because the API does not 133 // support fetching by ID. It fetches all connections and filters by ID and returns a fake 404 if nothing is found. 134 func (h *AzurePrivatelinkHandler) ConnectionGet(project, serviceName string, connID *string) (*AzurePrivatelinkConnectionResponse, error) { 135 conns, err := h.ConnectionsList(project, serviceName) 136 if err != nil { 137 return nil, err 138 } 139 140 var conn AzurePrivatelinkConnectionResponse 141 142 assertedConnID := PointerToString(connID) 143 if assertedConnID == "" { 144 assertedConnID = "0" 145 } else { 146 for _, it := range conns.Connections { 147 if it.PrivatelinkConnectionID == assertedConnID { 148 conn = it 149 break 150 } 151 } 152 } 153 154 if conn.PrivatelinkConnectionID == "" { 155 return nil, Error{ 156 Message: fmt.Sprintf("Azure Privatelink connection with the ID %s does not exists", assertedConnID), 157 Status: 404, 158 } 159 } 160 161 return &conn, nil 162 } 163 164 // ConnectionApprove approves an Azure Privatelink connection 165 func (h *AzurePrivatelinkHandler) ConnectionApprove(project, serviceName, privatelinkConnectionId string) error { 166 path := buildPath("project", project, "service", serviceName, "privatelink", "azure", "connections", privatelinkConnectionId, "approve") 167 rsp, err := h.client.doPostRequest(path, nil) 168 if err != nil { 169 return err 170 } 171 172 return checkAPIResponse(rsp, nil) 173 } 174 175 // ConnectionUpdate updates an Azure Privatelink connection 176 func (h *AzurePrivatelinkHandler) ConnectionUpdate(project, serviceName, privatelinkConnectionId string, req AzurePrivatelinkConnectionUpdateRequest) error { 177 path := buildPath("project", project, "service", serviceName, "privatelink", "azure", "connections", privatelinkConnectionId) 178 rsp, err := h.client.doPutRequest(path, req) 179 if err != nil { 180 return err 181 } 182 183 return checkAPIResponse(rsp, nil) 184 }