def create_port(network_id, ip_address, subnet_id, security_group_ids=None): ''' https://www.conoha.jp/docs/neutron-add_port.php ''' headers = { 'Accept': 'application/json', 'X-Auth-Token': config.get_token()['id'] } # 必須項目 data = { 'port': { 'network_id': network_id, 'fixed_ips': [{ 'ip_address': ip_address, 'subnet_id': subnet_id }] } } # Optional 項目 if security_group_ids is not None: data['port']['security_groups'] = security_group_ids return http.post(f'{endpoint}/ports', data, headers)
def create_network(): ''' https://www.conoha.jp/docs/neutron-add_network.php ''' headers = { 'Accept': 'application/json', 'X-Auth-Token': config.get_token()['id'] } return http.post(f'{endpoint}/networks', None, headers)
def create_subnet(network_id, cidr): ''' https://www.conoha.jp/docs/neutron-add_subnet.php ''' headers = { 'Accept': 'application/json', 'X-Auth-Token': config.get_token()['id'] } data = {'subnet': {'network_id': network_id, 'cidr': cidr}} return http.post(f'{endpoint}/subnets', data, headers)
def start_server(server_id): ''' https://www.conoha.jp/docs/compute-power_on_vm.php ''' headers = { 'Accept': 'application/json', 'X-Auth-Token': config.get_token()['id'] } data = {'os-start': None} return http.post(f'{endpoint}/servers/{server_id}/action', data, headers)
def attach_port(server_id, port_id): ''' https://www.conoha.jp/docs/compute-attach_port.php ''' headers = { 'Accept': 'application/json', 'X-Auth-Token': config.get_token()['id'] } data = {'interfaceAttachment': {'port_id': port_id}} return http.post(f'{endpoint}/servers/{server_id}/os-interface', data, headers)
def stop_server(server_id, force): ''' 通常停止: https://www.conoha.jp/docs/compute-stop_cleanly_vm.php 強制停止: https://www.conoha.jp/docs/compute-stop_forcibly_vm.php ''' headers = { 'Accept': 'application/json', 'X-Auth-Token': config.get_token()['id'] } data = {'os-stop': None} if force: data['os-stop'] = {'force_shutdown': True} return http.post(f'{endpoint}/servers/{server_id}/action', data, headers)
def get_token(user_name, password, tenant_id): ''' https://www.conoha.jp/docs/identity-post_tokens.php ''' headers = {'Accept': 'application/json'} data = { 'auth': { 'passwordCredentials': { 'username': user_name, 'password': password }, 'tenantId': tenant_id } } return http.post(f'{endpoint}/tokens', data, headers)
def create_security_group(name, description=None): ''' https://www.conoha.jp/docs/neutron-create_secgroup.php ''' headers = { 'Accept': 'application/json', 'X-Auth-Token': config.get_token()['id'] } # 必須項目 data = {'security_group': {'name': name}} # Optional 項目 if description is not None: data['security_group']['description'] = description return http.post(f'{endpoint}/security-groups', data, headers)
def create_server(image_ref, flavor_ref, admin_pass=None, key_name=None, security_groups=None, instance_name_tag=None, volume_id=None, vnc_keymap=None, user_data=None): ''' https://www.conoha.jp/docs/compute-create_vm.php ''' headers = { 'Accept': 'application/json', 'X-Auth-Token': config.get_token()['id'] } # 必須項目 data = {'server': {'imageRef': image_ref, 'flavorRef': flavor_ref}} # Optional 項目 if admin_pass is not None: data['server']['adminPass'] = admin_pass if key_name is not None: data['server']['key_name'] = key_name if security_groups is not None: data['server']['security_groups'] = [] for security_group in security_groups: data['server']['security_groups'].append({'name': security_group}) if instance_name_tag is not None: data['server']['metadata'] = {'instance_name_tag': instance_name_tag} if volume_id is not None: data['server']['block_device_mapping'] = {'volume_id': volume_id} if vnc_keymap is not None: data['server']['vncKeymap'] = vnc_keymap if user_data is not None: # 生の文字列を BASE64 エンコードに変換する data['server']['user_data'] = base64.b64encode( user_data.encode(encoding='utf-8')).decode(encoding='utf-8') return http.post(f'{endpoint}/servers', data, headers)
def create_security_group_rule(direction, ether_type, security_group_id, port_range_min=None, port_range_max=None, protocol=None, remote_group_id=None, remote_ip_prefix=None): ''' https://www.conoha.jp/docs/neutron-create_rule_on_secgroup.php ''' headers = { 'Accept': 'application/json', 'X-Auth-Token': config.get_token()['id'] } # 必須項目 data = { 'security_group_rule': { 'direction': direction, 'ethertype': ether_type, 'security_group_id': security_group_id } } # Optional 項目 if port_range_min is not None: data['security_group_rule']['port_range_min'] = port_range_min if port_range_max is not None: data['security_group_rule']['port_range_max'] = port_range_max if protocol is not None and protocol != 'null': data['security_group_rule']['protocol'] = protocol if remote_group_id is not None: data['security_group_rule']['remote_group_id'] = remote_group_id if remote_ip_prefix is not None: data['security_group_rule']['remote_ip_prefix'] = remote_ip_prefix return http.post(f'{endpoint}/security-group-rules', data, headers)