Skip to content

Commit

Permalink
Merge pull request #43 from in-seo/refactor/instance
Browse files Browse the repository at this point in the history
Upgrade Instance Metadata
  • Loading branch information
junho100 authored Aug 12, 2024
2 parents cfb48d0 + f057136 commit 16339bb
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 14 deletions.
14 changes: 14 additions & 0 deletions src/plugin/connector/block_storage/block_storage_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,17 @@ def get_volumes(self, secret_data: dict, region: REGION) -> list:
break

return volumes

def get_volume_detail(self, secret_data: dict, region: REGION, volume_id: str) -> list:
token = self.get_token(secret_data)

url = f"https://{region.name.lower()}-api-block-storage-infrastructure.nhncloudservice.com/v2/{secret_data.get('tenant_id')}/volumes/{volume_id}"
headers = {"X-Auth-Token": token}

response = requests.get(url, headers=headers)

if response.status_code != 200:
_LOGGER.error(f"Failed to get Block Storage Volumes. {response.json()}")
raise Exception(f"Failed to get Block Storage Volumes. {response.json()}")

return response.json().get('volume', [])
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ class SGConnector(NHNCloudBaseConnector):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def get_security_groups(self, secret_data: dict, region: REGION) -> dict:
def get_security_groups(self, secret_data: dict, region: REGION, name: str = None) -> dict:
token = self.get_token(secret_data)

url = f"https://{region.name.lower()}-api-network-infrastructure.nhncloudservice.com/v2.0/security-groups?tenant_id={secret_data.get('tenant_id')}"

# name이 제공되었으면 쿼리 파라미터에 추가
if name:
url += f"&name={name}"
headers = {"X-Auth-Token": token}

response = requests.get(url, headers=headers)

if response.status_code != 200:
_LOGGER.error(f"Failed to get Security Groups. {response.json()}")
raise Exception(f"Failed to get Security Groups. {response.json()}")
Expand Down
35 changes: 33 additions & 2 deletions src/plugin/manager/instance/instance_manager.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import json
import logging
from spaceone.inventory.plugin.collector.lib import *

from plugin.conf.cloud_service_conf import AUTH_TYPE, REGION, ASSET_URL
from plugin.connector.block_storage.block_storage_connector import BlockStorageConnector
from plugin.connector.instance.instance_connector import InstanceConnector
from plugin.connector.security_group.security_group_connector import SGConnector
from plugin.manager.base import NHNCloudBaseManager

_LOGGER = logging.getLogger("cloudforet")
Expand Down Expand Up @@ -38,16 +41,26 @@ def create_cloud_service_type(self):

def create_cloud_service(self, secret_data):
instance_connector = InstanceConnector()
block_connector = BlockStorageConnector()
sg_connector = SGConnector()
for AVAILABLE_REGION in self.AVAILABLE_REGIONS:
resources = instance_connector.get_servers(secret_data, AVAILABLE_REGION)
for resource in resources:
volume_list = resource['os-extended-volumes:volumes_attached']

volume_info_list = []
for volume in volume_list:
volume_info = instance_connector.get_volume(resource['id'], volume['id'], secret_data, AVAILABLE_REGION)
volume_info = block_connector.get_volume_detail(secret_data, AVAILABLE_REGION, volume['id'])
volume_info_list.append(volume_info)
resource['volumes'] = volume_info_list

network_list = resource['addresses'].keys() # addresses 이름이 동적이기때문에 직접 커스텀
network_info_list = []
for network_name in network_list:
network_info_list.append(resource['addresses'][network_name])
resource['networks'] = network_info_list

self.make_security_group_response(AVAILABLE_REGION, resource, secret_data, sg_connector)

reference = {
"resource_id": resource.get("id"),
"external_link": resource['links'][0]['href']
Expand All @@ -63,3 +76,21 @@ def create_cloud_service(self, secret_data):
region_code=AVAILABLE_REGION.name
)
yield cloud_service

def make_security_group_response(self, AVAILABLE_REGION, resource, secret_data, sg_connector):
sg_list = []
security_group_list = resource['security_groups']

for sg in security_group_list:
sg_details_list = sg_connector.get_security_groups(secret_data, AVAILABLE_REGION, sg['name'])
for sg_details in sg_details_list:
security_group_name = sg_details['name']
security_group_id = sg_details['id']

# 각 security_group_rule에 security_group의 id와 name을 추가
for rule in sg_details['security_group_rules']:
rule['group_id'] = security_group_id
rule['group_name'] = security_group_name

sg_list.append(sg_details)
resource['security_groups'] = sg_list
38 changes: 28 additions & 10 deletions src/plugin/metadata/instance/instance.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,14 @@ tabs.1:
type: table
root_path: data.volumes
fields:
- Name: name
- ID: id
- Device: device
- Server ID: serverId
- Volume ID: volumeId
- Volume Type: volume_type
- Size: size
- Description: description
- Status: status
- Availability Zone: availability_zone
- Created At: created_at

tabs.2:
name: Operating System
Expand All @@ -188,22 +192,36 @@ tabs.2:
- OS Distribution: os_version
- OS Architecture: os_architecture


tabs.3:
name: Network
type: table
root_path: data.addresses.vpc2
root_path: data.networks
fields:
- MAC Address: OS-EXT-IPS-MAC:mac_addr
- IP Address: addr
- Type: OS-EXT-IPS:type
- IP Version: version

tabs.4:
name: Security Groups
type: table
root_path: data.security_groups
fields:
- Name: name
items:
- name: Security Groups
type: table
root_path: data.security_groups.security_group_rules
fields:
- Security Group ID: group_id
- Security Group Name: group_name
- Rule ID: id
- Direction: direction
type: enum
enums:
- ingress: coral.600
- egress: indigo.500
- Protocol: protocol
- Port Range Min: port_range_min
- Port Range Max: port_range_max
- EtherType: ethertype
- Remote IP Prefix: remote_ip_prefix

tabs.5:
name: Metadata
Expand All @@ -215,4 +233,4 @@ tabs.5:
- Monitoring Agent: monitoring_agent
- Image Name: image_name
- Login Username: login_username
- TC Environment: tc_env
- TC Environment: tc_env

0 comments on commit 16339bb

Please sign in to comment.