-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add cloud service 'public ip prefixes'
Signed-off-by: ImMin5 <[email protected]>
- Loading branch information
Showing
8 changed files
with
220 additions
and
5 deletions.
There are no files selected for viewing
Empty file.
14 changes: 14 additions & 0 deletions
14
src/plugin/connector/public_ip_prefixes/public_ip_prefixes_connector.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import logging | ||
|
||
from plugin.connector.base import AzureBaseConnector | ||
|
||
_LOGGER = logging.getLogger("spaceone") | ||
|
||
|
||
class PublicIPPrefixesConnector(AzureBaseConnector): | ||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
self.set_connect(kwargs.get("secret_data")) | ||
|
||
def list_all_public_ip_prefixes(self): | ||
return self.network_client.public_ip_prefixes.list_all() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .ip_address_manager import PublicIPPrefixesManager |
114 changes: 114 additions & 0 deletions
114
src/plugin/manager/public_ip_prefiexes/ip_address_manager.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import logging | ||
|
||
from spaceone.inventory.plugin.collector.lib import * | ||
|
||
from plugin.conf.cloud_service_conf import ICON_URL | ||
from plugin.connector.public_ip_prefixes.public_ip_prefixes_connector import ( | ||
PublicIPPrefixesConnector, | ||
) | ||
|
||
from plugin.connector.subscriptions.subscriptions_connector import ( | ||
SubscriptionsConnector, | ||
) | ||
from plugin.manager.base import AzureBaseManager | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class PublicIPPrefixesManager(AzureBaseManager): | ||
cloud_service_group = "PublicIPPrefixes" | ||
cloud_service_type = "IPPrefix" | ||
service_code = "/Microsoft.Network/publicIPPrefixes" | ||
|
||
def create_cloud_service_type(self): | ||
return make_cloud_service_type( | ||
name=self.cloud_service_type, | ||
group=self.cloud_service_group, | ||
provider=self.provider, | ||
service_code=self.service_code, | ||
metadata_path=self.get_metadata_path(), | ||
is_primary=True, | ||
is_major=True, | ||
labels=["Networking"], | ||
tags={"spaceone:icon": f"{ICON_URL}/azure-public-ip-prefix.svg"}, | ||
) | ||
|
||
def create_cloud_service(self, options, secret_data, schema): | ||
cloud_services = [] | ||
error_responses = [] | ||
|
||
public_ip_prefixes_conn = PublicIPPrefixesConnector(secret_data=secret_data) | ||
subscription_conn = SubscriptionsConnector(secret_data=secret_data) | ||
|
||
subscription_obj = subscription_conn.get_subscription( | ||
secret_data["subscription_id"] | ||
) | ||
subscription_info = self.convert_nested_dictionary(subscription_obj) | ||
|
||
public_ip_prefixes_list = public_ip_prefixes_conn.list_all_public_ip_prefixes() | ||
|
||
for ip_address in public_ip_prefixes_list: | ||
|
||
try: | ||
ip_prefix_dict = self.convert_nested_dictionary(ip_address) | ||
ip_address_id = ip_prefix_dict["id"] | ||
|
||
ip_prefix_dict = self.update_tenant_id_from_secret_data( | ||
ip_prefix_dict, secret_data | ||
) | ||
ip_prefix_dict.update( | ||
{ | ||
"resource_group": self.get_resource_group_from_id( | ||
ip_address_id | ||
), | ||
"subscription_id": subscription_info["subscription_id"], | ||
"subscription_name": subscription_info["display_name"], | ||
"azure_monitor": {"resource_id": ip_address_id}, | ||
} | ||
) | ||
|
||
prefix_length = ip_prefix_dict.get("prefix_length") | ||
available_ip_address_count = self._get_available_ip_address_count( | ||
prefix_length | ||
) | ||
|
||
ip_prefix_dict["available_ip_address_count_display"] = ( | ||
available_ip_address_count | ||
) | ||
|
||
self.set_region_code(ip_prefix_dict["location"]) | ||
|
||
cloud_services.append( | ||
make_cloud_service( | ||
name=ip_prefix_dict["name"], | ||
cloud_service_type=self.cloud_service_type, | ||
cloud_service_group=self.cloud_service_group, | ||
provider=self.provider, | ||
data=ip_prefix_dict, | ||
account=secret_data["subscription_id"], | ||
instance_type=ip_prefix_dict["sku"]["name"], | ||
tags=ip_prefix_dict.get("tags", {}), | ||
region_code=ip_prefix_dict["location"], | ||
reference=self.make_reference(ip_prefix_dict.get("id")), | ||
data_format="dict", | ||
) | ||
) | ||
|
||
except Exception as e: | ||
_LOGGER.error( | ||
f"[create_cloud_service] Error {self.service} {e}", exc_info=True | ||
) | ||
error_responses.append( | ||
make_error_response( | ||
error=e, | ||
provider=self.provider, | ||
cloud_service_group=self.cloud_service_group, | ||
cloud_service_type=self.cloud_service_type, | ||
) | ||
) | ||
|
||
return cloud_services, error_responses | ||
|
||
@staticmethod | ||
def _get_available_ip_address_count(prefix_length: int) -> int: | ||
return 2 ** (32 - prefix_length) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
search: | ||
fields: | ||
- Subscription ID: account | ||
- Subscription Name: data.subscription_name | ||
- Resource Group: data.resource_group | ||
- Location: data.location | ||
- SKU: data.sku.name | ||
- Tier: data.sku.tier | ||
- IP Prefix: data.ip_prefix | ||
- Public IP Version: data.public_ip_address_version | ||
|
||
table: | ||
sort: | ||
key: data.id | ||
desc: false | ||
fields: | ||
- Available IPs: data.available_ip_address_count_display | ||
- Resource Group: data.resource_group | ||
- Subscription Name: data.subscription_name | ||
- Subscription ID: account | ||
- SKU: data.sku.name | ||
- SKU Tier: data.sku.tier | ||
|
||
|
||
|
||
tabs.0: | ||
name: Public IP Prefix | ||
type: item | ||
fields: | ||
- Name: name | ||
- Resource ID: data.id | ||
- Subscription: data.subscription_name | ||
- Subscription ID: data.subscription_id | ||
- Resource Group: data.resource_group | ||
- Location: data.location | ||
- SKU: data.sku.name | ||
- SKU Tier: data.sku.tier | ||
- IP Prefix: data.ip_prefix | ||
- Provisioning State: data.provisioning_state | ||
- Public IP Version: data.public_ip_address_version | ||
- Available IPs: data.available_ip_address_count_display | ||
- Prefix Length: data.prefix_length | ||
|
35 changes: 35 additions & 0 deletions
35
src/plugin/metrics/PublicIPPrefixes/IPAddress/ip_prefixes_count.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
metric_id: metric-azure-pubipprefixes-ipprefix-count | ||
name: IP Address Count | ||
metric_type: GAUGE | ||
resource_type: inventory.CloudService:azure.PublicIPPrefixes.IPPrefix | ||
query_options: | ||
group_by: | ||
- key: region_code | ||
name: Region | ||
reference: | ||
resource_type: inventory.Region | ||
reference_key: region_code | ||
- key: data.sku.name | ||
name: Sku name | ||
- key: data.sku.tier | ||
name: Sku Tier | ||
- key: data.tenant_id | ||
name: Tenant ID | ||
- key: data.subscription_name | ||
name: Subscription Name | ||
default: true | ||
- key: account | ||
name: Subscription ID | ||
- key: data.resource_group | ||
name: Resource Group | ||
- key: data.provisioning_state | ||
name: Provisioning State | ||
- key: data.public_ip_address_version | ||
name: Public IP Address Version | ||
fields: | ||
value: | ||
operator: count | ||
unit: Count | ||
namespace_id: ns-azure-pubipprefixes-ipprefixes | ||
version: '1.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
namespace_id: ns-azure-pubipprefixes-ipprefix | ||
name: PublicIPPrefixes/IPPrefix | ||
resource_type: inventory.CloudService:azure.PublicIPPrefixes.IPPrefix | ||
group: azure | ||
category: ASSET | ||
icon: https://spaceone-custom-assets.s3.ap-northeast-2.amazonaws.com/console-assets/icons/cloud-services/azure/azure-public-ip-prefix.svg | ||
version: '1.0' |