Skip to content

Commit

Permalink
feat(ipam): add v1 API (scaleway#938)
Browse files Browse the repository at this point in the history
Co-authored-by: Yacine FODIL <[email protected]>
  • Loading branch information
scaleway-bot and yfodil authored Oct 19, 2023
1 parent 8a1c1ee commit 915f755
Show file tree
Hide file tree
Showing 7 changed files with 545 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/clients/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * as Function from './function'
export * as IAM from './iam'
export * as Instance from './instance'
export * as IOT from './iot'
export * as IPAM from './ipam'
export * as IPFS from './ipfs'
export * as K8S from './k8s'
export * as LB from './lb'
Expand Down
1 change: 1 addition & 0 deletions packages/clients/src/api/ipam/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as v1 from './v1/index.gen'
174 changes: 174 additions & 0 deletions packages/clients/src/api/ipam/v1/api.gen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
// This file was automatically generated. DO NOT EDIT.
// If you have any remark or suggestion do not hesitate to open an issue.
import {
API as ParentAPI,
enrichForPagination,
resolveOneOf,
urlParams,
validatePathParam,
} from '../../../bridge'
import type { Region } from '../../../bridge'
import {
marshalBookIPRequest,
marshalUpdateIPRequest,
unmarshalIP,
unmarshalListIPsResponse,
} from './marshalling.gen'
import type {
BookIPRequest,
GetIPRequest,
IP,
ListIPsRequest,
ListIPsResponse,
ReleaseIPRequest,
UpdateIPRequest,
} from './types.gen'

const jsonContentHeaders = {
'Content-Type': 'application/json; charset=utf-8',
}

/**
* IPAM API.
*
* This API allows you to manage IP addresses with Scaleway's IP Address
* Management tool. IPAM API.
*/
export class API extends ParentAPI {
/** Lists the available regions of the API. */
public static readonly LOCALITIES: Region[] = ['fr-par', 'nl-ams', 'pl-waw']

/**
* Book a new IP. Book a new IP from the specified source. Currently IPs can
* only be booked from a Private Network.
*
* @param request - The request {@link BookIPRequest}
* @returns A Promise of IP
*/
bookIP = (request: Readonly<BookIPRequest>) =>
this.client.fetch<IP>(
{
body: JSON.stringify(
marshalBookIPRequest(request, this.client.settings),
),
headers: jsonContentHeaders,
method: 'POST',
path: `/ipam/v1/regions/${validatePathParam(
'region',
request.region ?? this.client.settings.defaultRegion,
)}/ips`,
},
unmarshalIP,
)

/**
* Release an IP. Release an IP not currently attached to a resource, and
* returns it to the available IP pool.
*
* @param request - The request {@link ReleaseIPRequest}
*/
releaseIP = (request: Readonly<ReleaseIPRequest>) =>
this.client.fetch<void>({
body: '{}',
headers: jsonContentHeaders,
method: 'DELETE',
path: `/ipam/v1/regions/${validatePathParam(
'region',
request.region ?? this.client.settings.defaultRegion,
)}/ips/${validatePathParam('ipId', request.ipId)}`,
})

/**
* Get an IP. Retrieve details of an existing IP, specified by its IP ID.
*
* @param request - The request {@link GetIPRequest}
* @returns A Promise of IP
*/
getIP = (request: Readonly<GetIPRequest>) =>
this.client.fetch<IP>(
{
method: 'GET',
path: `/ipam/v1/regions/${validatePathParam(
'region',
request.region ?? this.client.settings.defaultRegion,
)}/ips/${validatePathParam('ipId', request.ipId)}`,
},
unmarshalIP,
)

/**
* Update an IP. Update parameters including tags of the specified IP.
*
* @param request - The request {@link UpdateIPRequest}
* @returns A Promise of IP
*/
updateIP = (request: Readonly<UpdateIPRequest>) =>
this.client.fetch<IP>(
{
body: JSON.stringify(
marshalUpdateIPRequest(request, this.client.settings),
),
headers: jsonContentHeaders,
method: 'PATCH',
path: `/ipam/v1/regions/${validatePathParam(
'region',
request.region ?? this.client.settings.defaultRegion,
)}/ips/${validatePathParam('ipId', request.ipId)}`,
},
unmarshalIP,
)

protected pageOfListIPs = (request: Readonly<ListIPsRequest> = {}) =>
this.client.fetch<ListIPsResponse>(
{
method: 'GET',
path: `/ipam/v1/regions/${validatePathParam(
'region',
request.region ?? this.client.settings.defaultRegion,
)}/ips`,
urlParams: urlParams(
['attached', request.attached],
['is_ipv6', request.isIpv6],
['mac_address', request.macAddress],
['order_by', request.orderBy ?? 'created_at_desc'],
['organization_id', request.organizationId],
['page', request.page],
[
'page_size',
request.pageSize ?? this.client.settings.defaultPageSize,
],
['project_id', request.projectId],
['resource_id', request.resourceId],
['resource_name', request.resourceName],
['resource_type', request.resourceType ?? 'unknown_type'],
['tags', request.tags],
...Object.entries(
resolveOneOf([
{
param: 'zonal',
value: request.zonal,
},
{
param: 'private_network_id',
value: request.privateNetworkId,
},
]),
),
),
},
unmarshalListIPsResponse,
)

/**
* List existing IPs. List existing IPs in the specified region using various
* filters. For example, you can filter for IPs within a specified Private
* Network, or for public IPs within a specified Project. By default, the IPs
* returned in the list are ordered by creation date in ascending order,
* though this can be modified via the order_by field.
*
* @param request - The request {@link ListIPsRequest}
* @returns A Promise of ListIPsResponse
*/
listIPs = (request: Readonly<ListIPsRequest> = {}) =>
enrichForPagination('ips', this.pageOfListIPs, request)
}
17 changes: 17 additions & 0 deletions packages/clients/src/api/ipam/v1/index.gen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// This file was automatically generated. DO NOT EDIT.
// If you have any remark or suggestion do not hesitate to open an issue.
export { API } from './api.gen'
export type {
BookIPRequest,
GetIPRequest,
IP,
ListIPsRequest,
ListIPsRequestOrderBy,
ListIPsResponse,
ReleaseIPRequest,
Resource,
ResourceType,
Source,
UpdateIPRequest,
} from './types.gen'
export * as ValidationRules from './validation-rules.gen'
119 changes: 119 additions & 0 deletions packages/clients/src/api/ipam/v1/marshalling.gen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// This file was automatically generated. DO NOT EDIT.
// If you have any remark or suggestion do not hesitate to open an issue.
import {
isJSONObject,
resolveOneOf,
unmarshalArrayOfObject,
unmarshalDate,
} from '../../../bridge'
import type { DefaultValues } from '../../../bridge'
import type {
BookIPRequest,
IP,
ListIPsResponse,
Resource,
Source,
UpdateIPRequest,
} from './types.gen'

const unmarshalResource = (data: unknown) => {
if (!isJSONObject(data)) {
throw new TypeError(
`Unmarshalling the type 'Resource' failed as data isn't a dictionary.`,
)
}

return {
id: data.id,
macAddress: data.mac_address,
name: data.name,
type: data.type,
} as Resource
}

const unmarshalSource = (data: unknown) => {
if (!isJSONObject(data)) {
throw new TypeError(
`Unmarshalling the type 'Source' failed as data isn't a dictionary.`,
)
}

return {
privateNetworkId: data.private_network_id,
subnetId: data.subnet_id,
zonal: data.zonal,
} as Source
}

export const unmarshalIP = (data: unknown) => {
if (!isJSONObject(data)) {
throw new TypeError(
`Unmarshalling the type 'IP' failed as data isn't a dictionary.`,
)
}

return {
address: data.address,
createdAt: unmarshalDate(data.created_at),
id: data.id,
isIpv6: data.is_ipv6,
projectId: data.project_id,
region: data.region,
resource: data.resource ? unmarshalResource(data.resource) : undefined,
source: data.source ? unmarshalSource(data.source) : undefined,
tags: data.tags,
updatedAt: unmarshalDate(data.updated_at),
zone: data.zone,
} as IP
}

export const unmarshalListIPsResponse = (data: unknown) => {
if (!isJSONObject(data)) {
throw new TypeError(
`Unmarshalling the type 'ListIPsResponse' failed as data isn't a dictionary.`,
)
}

return {
ips: unmarshalArrayOfObject(data.ips, unmarshalIP),
totalCount: data.total_count,
} as ListIPsResponse
}

const marshalSource = (
request: Source,
defaults: DefaultValues,
): Record<string, unknown> => ({
...resolveOneOf([
{
param: 'zonal',
value: request.zonal,
},
{
param: 'private_network_id',
value: request.privateNetworkId,
},
{
param: 'subnet_id',
value: request.subnetId,
},
]),
})

export const marshalBookIPRequest = (
request: BookIPRequest,
defaults: DefaultValues,
): Record<string, unknown> => ({
address: request.address,
is_ipv6: request.isIpv6,
project_id: request.projectId ?? defaults.defaultProjectId,
source: request.source ? marshalSource(request.source, defaults) : undefined,
tags: request.tags,
})

export const marshalUpdateIPRequest = (
request: UpdateIPRequest,
defaults: DefaultValues,
): Record<string, unknown> => ({
tags: request.tags,
})
Loading

0 comments on commit 915f755

Please sign in to comment.