def test_ip_in_server_creation(self):
        """IPAddress in server creation.

        https://www.upcloud.com/api/8-servers/#create-server
        """
        ip1 = IPAddress(family='IPv4', access='public')
        ip2 = IPAddress(family='IPv6', access='private')
        assert ip1.to_dict() == {'family': 'IPv4', 'access': 'public'}
        assert ip2.to_dict() == {'family': 'IPv6', 'access': 'private'}
    def test_ip_details(self):
        """IPAdress LIST/GET.

        https://www.upcloud.com/api/10-ip-addresses/#list-ip-addresses
        """
        ip = IPAddress(**json.loads(read_from_file('ip_address_10.1.0.101.json'))['ip_address'])
        assert ip.to_dict() == {
            'access': 'private',
            'address': '10.1.0.101',
            'family': 'IPv4',
            'part_of_plan': 'yes',
            'ptr_record': 'a.ptr.record',
            'server': '008c365d-d307-4501-8efc-cd6d3bb0e494'
        }
예제 #3
0
    def test_ip_details(self):
        """IPAdress LIST/GET.

        https://www.upcloud.com/api/10-ip-addresses/#list-ip-addresses
        """
        ip = IPAddress(**json.loads(
            read_from_file('ip_address_10.1.0.101.json'))['ip_address'])
        assert ip.to_dict() == {
            'access': 'private',
            'address': '10.1.0.101',
            'family': 'IPv4',
            'part_of_plan': 'yes',
            'ptr_record': 'a.ptr.record',
            'server': '008c365d-d307-4501-8efc-cd6d3bb0e494'
        }
예제 #4
0
    def modify_server(self, UUID, **kwargs):
        """
        modify_server allows updating the server's updateable_fields.

        Note: Server's IP-addresses and Storages are managed by their own add/remove methods.
        """
        body = dict()
        body['server'] = {}
        for arg in kwargs:
            if arg not in Server.updateable_fields:
                Exception('{0} is not an updateable field'.format(arg))
            body['server'][arg] = kwargs[arg]

        res = self.put_request('/server/{0}'.format(UUID), body)
        server = res['server']

        # Populate subobjects
        IPAddresses = IPAddress._create_ip_address_objs(
            server.pop('ip_addresses'), cloud_manager=self)

        storages = Storage._create_storage_objs(server.pop('storage_devices'),
                                                cloud_manager=self)

        return Server(server,
                      ip_addresses=IPAddresses,
                      storage_devices=storages,
                      populated=True,
                      cloud_manager=self)
 def get_ips(self):
     """
     Get all IPAddress objects from the API.
     """
     res = self.get_request('/ip_address')
     IPs = IPAddress._create_ip_address_objs(res['ip_addresses'], cloud_manager=self)
     return IPs
    def test_ip_in_server_details(self):
        """IPAddress in server details.

        https://www.upcloud.com/api/8-servers/#get-server-details
        """
        ip = IPAddress(access='private', address='10.0.0.0', family='IPv4')
        assert ip.to_dict() == {
            'access': 'private',
            'address': '10.0.0.0',
            'family' : 'IPv4'
        }

        data = read_from_file('server_00798b85-efdc-41ca-8021-f6ef457b8531.json')
        s = Server(**json.loads(data))
        for ip in s.ip_addresses:
            assert set(ip.to_dict().keys()) == set(['address', 'family', 'access'])
    def modify_server(self, UUID, **kwargs):
        """
        modify_server allows updating the server's updateable_fields.

        Note: Server's IP-addresses and Storages are managed by their own add/remove methods.
        """
        body = dict()
        body['server'] = {}
        for arg in kwargs:
            if arg not in Server.updateable_fields:
                Exception('{0} is not an updateable field'.format(arg))
            body['server'][arg] = kwargs[arg]

        res = self.request('PUT', '/server/{0}'.format(UUID), body)
        server = res['server']

        # Populate subobjects
        IPAddresses = IPAddress._create_ip_address_objs(server.pop('ip_addresses'),
                                                          cloud_manager=self)

        storages = Storage._create_storage_objs(server.pop('storage_devices'),
                                                cloud_manager=self)

        return Server(
            server,
            ip_addresses=IPAddresses,
            storage_devices=storages,
            populated=True,
            cloud_manager=self
        )
예제 #8
0
 def get_ips(self, ignore_ips_without_server=False):
     """
     Get all IPAddress objects from the API.
     """
     res = self.get_request('/ip_address')
     IPs = IPAddress._create_ip_address_objs(res['ip_addresses'], self,
                                             ignore_ips_without_server)
     return IPs
예제 #9
0
 def get_ips(self):
     """
     Get all IPAddress objects from the API.
     """
     res = self.get_request('/ip_address')
     IPs = IPAddress._create_ip_address_objs(res['ip_addresses'],
                                             cloud_manager=self)
     return IPs
예제 #10
0
    def attach_ip(self, server, family='IPv4'):
        """
        Attach a new (random) IPAddress to the given server (object or UUID).
        """
        body = {'ip_address': {'server': str(server), 'family': family}}

        res = self.post_request('/ip_address', body)
        return IPAddress(cloud_manager=self, **res['ip_address'])
예제 #11
0
    def get_ip(self, address):
        """
        Get an IPAddress object with the IP address (string) from the API.

        e.g manager.get_ip('80.69.175.210')
        """
        res = self.get_request('/ip_address/' + address)
        return IPAddress(cloud_manager=self, **res['ip_address'])
예제 #12
0
    def test_ip_in_server_details(self):
        """IPAddress in server details.

        https://www.upcloud.com/api/8-servers/#get-server-details
        """
        ip = IPAddress(access='private', address='10.0.0.0', family='IPv4')
        assert ip.to_dict() == {
            'access': 'private',
            'address': '10.0.0.0',
            'family': 'IPv4'
        }

        data = read_from_file(
            'server_00798b85-efdc-41ca-8021-f6ef457b8531.json')
        s = Server(**json.loads(data))
        for ip in s.ip_addresses:
            assert set(ip.to_dict().keys()) == set(
                ['address', 'family', 'access'])
예제 #13
0
    def modify_ip(self, ip_addr, ptr_record):
        """
        Modify an IP address' ptr-record (Reverse DNS).

        Accepts an IPAddress instance (object) or its address (string).
        """
        body = {'ip_address': {'ptr_record': ptr_record}}

        res = self.put_request('/ip_address/' + str(ip_addr), body)
        return IPAddress(cloud_manager=self, **res['ip_address'])
예제 #14
0
    def get_server_data(self, uuid):
        server_data = {}
        servers = self.read_json_data("server_populated").get("servers").get(
            "server")
        for server in servers:
            if server.get("uuid") == uuid:
                server_data = server
        IPAddresses = IPAddress._create_ip_address_objs(
            server.pop("ip_addresses"), cloud_manager=self)

        storages = Storage._create_storage_objs(server.pop("storage_devices"),
                                                cloud_manager=self)
        return server_data, IPAddresses, storages
예제 #15
0
    def test_ip_in_server_creation(self):
        """IPAddress in server creation.

        https://www.upcloud.com/api/8-servers/#create-server
        """
        ip1 = IPAddress(family='IPv4', access='public')
        ip2 = IPAddress(family='IPv6', access='private')
        assert ip1.to_dict() == {'family': 'IPv4', 'access': 'public'}
        assert ip2.to_dict() == {'family': 'IPv6', 'access': 'private'}
예제 #16
0
    def _handle_server_subobjs(cls, server, cloud_manager):
        ip_data = server.pop('ip_addresses', None)
        storage_data = server.pop('storage_devices', None)
        tags = server.pop('tags', None)

        if ip_data:
            ip_addresses = IPAddress._create_ip_address_objs(ip_data, cloud_manager=cloud_manager)
            server['ip_addresses'] = ip_addresses

        if storage_data:
            storages = Storage._create_storage_objs(storage_data, cloud_manager=cloud_manager)
            server['storage_devices'] = storages

        if tags and 'tag' in tags:
            server['tags'] = tags['tag']
예제 #17
0
    def _handle_server_subobjs(cls, server, cloud_manager):
        ip_data = server.pop('ip_addresses', None)
        storage_data = server.pop('storage_devices', None)
        tags = server.pop('tags', None)

        if ip_data:
            ip_addresses = IPAddress._create_ip_address_objs(ip_data, cloud_manager=cloud_manager)
            server['ip_addresses'] = ip_addresses

        if storage_data:
            storages = Storage._create_storage_objs(storage_data, cloud_manager=cloud_manager)
            server['storage_devices'] = storages

        if tags and 'tag' in tags:
            server['tags'] = tags['tag']
    def get_server_data(self, UUID):
        """
        Return '/server/uuid' data in Python dict.

        Creates object representations of any IP-address and Storage.
        """
        data = self.get_request('/server/{0}'.format(UUID))
        server = data['server']

        # Populate subobjects
        IPAddresses = IPAddress._create_ip_address_objs(server.pop('ip_addresses'),
                                                          cloud_manager=self)

        storages = Storage._create_storage_objs(server.pop('storage_devices'),
                                                cloud_manager=self)

        return server, IPAddresses, storages
예제 #19
0
    def get_server_data(self, UUID):
        """
        Return '/server/uuid' data in Python dict.

        Creates object representations of any IP-address and Storage.
        """
        data = self.get_request('/server/{0}'.format(UUID))
        server = data['server']

        # Populate subobjects
        IPAddresses = IPAddress._create_ip_address_objs(
            server.pop('ip_addresses'), cloud_manager=self)

        storages = Storage._create_storage_objs(server.pop('storage_devices'),
                                                cloud_manager=self)

        return server, IPAddresses, storages
예제 #20
0
    def test_server_prepare_post_body_optional_attributes(self):
        server1 = Server(
            core_number=2,
            memory_amount=1024,
            hostname='my.example.com',
            zone='us-chi1',
            storage_devices=[
                Storage(
                    os='01000000-0000-4000-8000-000030200200',
                    size=10
                )
            ],
            vnc_password='******',
            password_delivery='email',
            login_user=login_user_block('upclouduser', ['this-is-a-SSH-key']),
            avoid_host='12345678',
            user_data='https://my.script.com/some_script.py',
            ip_addresses = [
                IPAddress(family='IPv4', access='public'),
                IPAddress(family='IPv6', access='public')
            ]
        )


        server2_dict = {
            'core_number':2,
            'memory_amount':1024,
            'hostname':'my.example.com',
            'zone': 'us-chi1',
            'storage_devices':[
                {'os': '01000000-0000-4000-8000-000030200200', 'size': 10}
            ],
            'vnc_password': '******',
            'password_delivery': 'email',
            'login_user': login_user_block('upclouduser', ['this-is-a-SSH-key']),
            'avoid_host': '12345678',
            'user_data': 'https://my.script.com/some_script.py',
            'ip_addresses': [
                {'family':'IPv4', 'access':'public'},
                {'family':'IPv6', 'access':'public'}
            ]
        }
        server2 = Server._create_server_obj(server2_dict, cloud_manager=self)

        body1 = server1.prepare_post_body()
        body2 = server2.prepare_post_body()


        for body in [body1, body2]:
            assert body['server']['title'] == 'my.example.com'
            assert body['server']['core_number'] == 2
            assert body['server']['memory_amount'] == 1024
            assert body['server']['hostname'] == server1.title
            assert body['server']['zone'] == 'us-chi1'
            assert body['server']['vnc_password'] == 'my-passwd'
            assert body['server']['password_delivery'] == 'email'
            assert body['server']['login_user'] == {
                'username': '******',
                'create_password': '******',
                'ssh_keys': {
                    'ssh_key': ['this-is-a-SSH-key']
                }
            }
            assert body['server']['avoid_host'] == '12345678'
            assert body['server']['user_data'] == 'https://my.script.com/some_script.py'
            assert body['server']['ip_addresses'] == {
                'ip_address': [
                    {'family': 'IPv4', 'access': 'public'},
                    {'family': 'IPv6', 'access': 'public'}
                ]
            }
예제 #21
0
            Storage(os='01000000-0000-4000-8000-000030060200', size=10),
            Storage(size=10, tier='maxiops')
        ]),

    'web2': Server(
        core_number=1,
        memory_amount=1024,
        hostname='web2.example.com',
        zone='uk-lon1',
        password_delivery='none',
        storage_devices=[
            Storage(os='01000000-0000-4000-8000-000030060200', size=10),
            Storage(size=10, tier='maxiops'),
        ],
        ip_addresses=[
            IPAddress(family='IPv6', access='public')
        ]),

    'db': Server(
        core_number=1,
        memory_amount=1024,
        hostname='db.example.com',
        zone='uk-lon1',
        password_delivery='none',
        storage_devices=[
            Storage(os='01000000-0000-4000-8000-000050010300', size=10),
            Storage(size=10),
        ],
        login_user=login_user_block('testuser', ['ssh-rsa AAAAB3NzaC1yc2EAA[...]ptshi44x [email protected]'], True),
        ),
예제 #22
0
        password_delivery='none',
        storage_devices=[
            Storage(os='Ubuntu 14.04', size=10),
            Storage(size=10, tier='maxiops')
        ]),
 'web2':
 Server(core_number=1,
        memory_amount=1024,
        hostname='web2.example.com',
        zone=ZONE.London,
        password_delivery='none',
        storage_devices=[
            Storage(os='Ubuntu 14.04', size=10),
            Storage(size=10, tier='maxiops'),
        ],
        ip_addresses=[IPAddress(family='IPv6', access='public')]),
 'db':
 Server(
     core_number=1,
     memory_amount=1024,
     hostname='db.example.com',
     zone=ZONE.London,
     password_delivery='none',
     storage_devices=[
         Storage(os='CentOS 7.6', size=10),
         Storage(size=10),
     ],
     login_user=login_user_block(
         'testuser',
         ['ssh-rsa AAAAB3NzaC1yc2EAA[...]ptshi44x [email protected]'], True),
 ),
예제 #23
0
 def get_ips(self, ignore_ips_without_server=False):
     data = self.read_json_data("ip_address")
     IPs = IPAddress._create_ip_address_objs(data.get("ip_addresses"), self,
                                             ignore_ips_without_server)
     return IPs