File: management_console.py

package info (click to toggle)
python-pypowervm 1.1.24%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 7,400 kB
  • sloc: python: 29,780; xml: 174; makefile: 14
file content (64 lines) | stat: -rw-r--r-- 2,107 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Copyright 2015 IBM Corp.
#
# All Rights Reserved.
#
#    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.

from pypowervm.utils import transaction as trans
from pypowervm.wrappers import management_console as mc


def get_public_key(adapter):
    """Get the public key for the management console.

    :param adapter: The adapter for the pypowervm API.
    :return: The public key
    """
    # Get the consoles feed and use the first, there is just one.
    console = mc.ManagementConsole.wrap(
        adapter.read(mc.ManagementConsole.schema_type))[0]
    return console.ssh_public_key


def add_authorized_key(adapter, public_key):
    """Add an authorized public key to the management console.

    The public_key will be added if it doesn't already exist.

    :param adapter: The adapter for the pypowervm API.
    :param public_key: The public key to be added.
    """
    console_w = mc.ManagementConsole.wrap(
        adapter.read(mc.ManagementConsole.schema_type))[0]

    @trans.entry_transaction
    def run_update(console):
        keys = console.ssh_authorized_keys
        if public_key not in keys:
            keys = list(keys)
            keys.append(public_key)
            console.ssh_authorized_keys = keys
            console.update()

    run_update(console_w)


def get_authorized_keys(adapter):
    """Get all authorized keys on the management console.

    :param adapter: The adapter for the pypowervm API.
    """
    console_w = mc.ManagementConsole.wrap(
        adapter.read(mc.ManagementConsole.schema_type))[0]

    return console_w.ssh_authorized_keys