Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat.] APIG API basic methods #537

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions doc/source/sdk/guides/apig.rst
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,43 @@ This example demonstrates how to get throttling policy.

.. literalinclude:: ../examples/apig/get_throttling_policy.py
:lines: 16-24

Creating an API
^^^^^^^^^^^^^^^

This example demonstrates how to create an API.

.. literalinclude:: ../examples/apig/create_api.py
:lines: 16-45

Modifying an API
^^^^^^^^^^^^^^^^

This example demonstrates how to update an API.

.. literalinclude:: ../examples/apig/update_api.py
:lines: 16-46

Deleting an API
^^^^^^^^^^^^^^^

This example demonstrates how to delete an API.

.. literalinclude:: ../examples/apig/delete_api.py
:lines: 16-23

Querying API Details
^^^^^^^^^^^^^^^^^^^^

This example demonstrates how to get an API.

.. literalinclude:: ../examples/apig/get_api.py
:lines: 16-24

Querying APIs
^^^^^^^^^^^^^

This example demonstrates how to list an APIs.

.. literalinclude:: ../examples/apig/list_apis.py
:lines: 16-20
6 changes: 6 additions & 0 deletions doc/source/sdk/proxies/apig.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@ Throttling Policies Operations
:members: throttling_policies, create_throttling_policy,
delete_throttling_policy, get_throttling_policy,
update_throttling_policy

Api Operations
^^^^^^^^^^^^^^
.. autoclass:: otcextensions.sdk.apig.v2._proxy.Proxy
:noindex:
:members: apis, create_api, delete_api, get_api, update_api
2 changes: 1 addition & 1 deletion doc/source/sdk/resources/apig/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ ApiGateway Resources
v2/group
v2/env_var
v2/throttling_policy

v2/api
13 changes: 13 additions & 0 deletions doc/source/sdk/resources/apig/v2/api.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
otcextensions.sdk.apig.v2.api
========================================

.. automodule:: otcextensions.sdk.apig.v2.api

The Api Class
------------------------

The ``Api`` class inherits from
:class:`~otcextensions.sdk.sdk_resource.Resource`.

.. autoclass:: otcextensions.sdk.apig.v2.api.Api
:members:
45 changes: 45 additions & 0 deletions examples/apig/create_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python3
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
Create API
"""
import openstack

openstack.enable_logging(True)
conn = openstack.connect(cloud='otc')
attrs = {
"group_id": "group_id",
"name": "test_api_001",
"auth_type": "IAM",
"backend_type": "HTTP",
"req_protocol": "HTTP",
"req_uri": "/test/http",
"remark": "Mock backend API",
"type": 2,
"req_method": "GET",
"result_normal_sample": "Example success response",
"result_failure_sample": "Example failure response",
"tags": ["httpApi"],
"backend_api": {
"req_protocol": "HTTP",
"req_method": "GET",
"req_uri": "/test/benchmark",
"timeout": 5000,
"retry_count": "-1",
"url_domain": "192.168.189.156:12346"
},
}
created = conn.apig.create_apicreate_api(
gateway="gateway_id",
**attrs
)
23 changes: 23 additions & 0 deletions examples/apig/delete_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python3
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
Delete API
"""
import openstack

openstack.enable_logging(True)
conn = openstack.connect(cloud='otc')
conn.apig.delete_api(
gateway="gateway_id",
api="env_id"
)
24 changes: 24 additions & 0 deletions examples/apig/get_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env python3
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
Get API
"""
import openstack

openstack.enable_logging(True)
conn = openstack.connect(cloud='otc')

conn.apig.get_api(
gateway="gateway_id",
api="api_id"
)
20 changes: 20 additions & 0 deletions examples/apig/list_apis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env python3
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
List APIs of specific gateway
"""
import openstack

openstack.enable_logging(True)
conn = openstack.connect(cloud='otc')
environments = list(conn.apig.apis(gateway="gateway_id"))
46 changes: 46 additions & 0 deletions examples/apig/update_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python3
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
Update API in gateway
"""
import openstack

openstack.enable_logging(True)
conn = openstack.connect(cloud='otc')
attrs = {
"group_id": "group_id",
"name": "test_api_001",
"auth_type": "IAM",
"backend_type": "HTTP",
"req_protocol": "HTTP",
"req_uri": "/test/http",
"remark": "Mock backend API",
"type": 2,
"req_method": "GET",
"result_normal_sample": "Example success response",
"result_failure_sample": "Example failure response",
"tags": ["httpApi"],
"backend_api": {
"req_protocol": "HTTP",
"req_method": "GET",
"req_uri": "/test/benchmark",
"timeout": 5000,
"retry_count": "-1",
"url_domain": "192.168.189.156:12346"
},
}
environment = conn.apig.update_api(
api="env_id",
gateway="gateway_id",
**attrs
)
86 changes: 86 additions & 0 deletions otcextensions/sdk/apig/v2/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from otcextensions.sdk.apig.v2 import apigroup as _api_group
from otcextensions.sdk.apig.v2 import apienvironmentvar as _api_var
from otcextensions.sdk.apig.v2 import throttling_policy as _tp
from otcextensions.sdk.apig.v2 import api as _api


class Proxy(proxy.Proxy):
Expand Down Expand Up @@ -613,3 +614,88 @@ def get_throttling_policy(self, gateway, policy):
policy,
gateway_id=gateway.id
)

# ======== Api Methods ========

def create_api(self, gateway, **attrs):
"""Create a new API for a specific API Gateway.

:param gateway: The ID of the gateway or an instance of
:class:`~otcextensions.sdk.apig.v2.gateway.Gateway`
:param attrs: Additional attributes for the Api creation.

:returns: An instance of Api
"""
gateway = self._get_resource(_gateway.Gateway, gateway)
return self._create(_api.Api,
gateway_id=gateway.id,
**attrs)

def update_api(self, gateway, api, **attrs):
"""Update an existing API for a specific API Gateway.

:param gateway: The ID of the gateway or an instance of
:class:`~otcextensions.sdk.apig.v2.gateway.Gateway`
:param api: The ID of the Api or an instance of Api
:param attrs: Additional attributes to update the Api.

:returns: Updated instance of Api
"""
gateway = self._get_resource(_gateway.Gateway, gateway)
api = self._get_resource(_api.Api, api)
return self._update(
_api.Api,
api,
gateway_id=gateway.id,
**attrs
)

def delete_api(self, gateway, api, ignore_missing=False):
"""Delete an existing API from a specific API Gateway.

:param gateway: The ID of the gateway or an instance of
:class:`~otcextensions.sdk.apig.v2.gateway.Gateway`
:param api: The ID of the Api or an instance of Api

:returns: None
"""
gateway = self._get_resource(_gateway.Gateway, gateway)
api = self._get_resource(_api.Api, api)
return self._delete(
_api.Api,
api,
gateway_id=gateway.id,
ignore_missing=ignore_missing
)

def apis(self, gateway, **attrs):
"""List all APIs for a specific API Gateway.

:param gateway: The ID of the gateway or an instance of
:class:`~otcextensions.sdk.apig.v2.gateway.Gateway`
:param attrs: Additional filters for listing Api.

:returns: A list of instances of Api
"""
gateway = self._get_resource(_gateway.Gateway, gateway)
return self._list(_api.Api,
paginated=False,
gateway_id=gateway.id,
**attrs)

def get_api(self, gateway, api):
"""Retrieve details of a specific API.

:param gateway: The ID of the gateway or an instance of
:class:`~otcextensions.sdk.apig.v2.gateway.Gateway`
:param api: The ID of the Api or an instance of Api

:returns: An instance of Api
"""
gateway = self._get_resource(_gateway.Gateway, gateway)
api = self._get_resource(_api.Api, api)
return self._get(
_api.Api,
api,
gateway_id=gateway.id
)
Loading