Refactoring of UserSettings to extend UserConfig
[snaps.git] / snaps / config / image.py
1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 #                    and others.  All rights reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16
17 class ImageConfig(object):
18     def __init__(self, **kwargs):
19         """
20         Constructor
21         :param name: the image's name (required)
22         :param image_user: the image's default sudo user (required)
23         :param format or img_format: the image format type (required)
24         :param url or download_url: the image download location (requires url
25                                     or img_file)
26         :param image_file: the image file location (requires url or img_file)
27         :param extra_properties: dict() object containing extra parameters to
28                                  pass when loading the image;
29                                  can be ids of kernel and initramfs images for
30                                  a 3-part image
31         :param nic_config_pb_loc: the file location to the Ansible Playbook
32                                   that can configure multiple NICs
33         :param kernel_image_settings: the settings for a kernel image
34         :param ramdisk_image_settings: the settings for a ramdisk image
35         :param exists: When True, an image with the given name must exist
36         :param public: When True, an image will be created with public
37                        visibility
38         """
39
40         self.name = kwargs.get('name')
41         self.image_user = kwargs.get('image_user')
42         self.format = kwargs.get('format')
43         if not self.format:
44             self.format = kwargs.get('img_format')
45
46         self.url = kwargs.get('url')
47         if not self.url:
48             self.url = kwargs.get('download_url')
49         if self.url == 'None':
50             self.url = None
51
52         self.image_file = kwargs.get('image_file')
53         if self.image_file == 'None':
54             self.image_file = None
55
56         self.extra_properties = kwargs.get('extra_properties')
57         self.nic_config_pb_loc = kwargs.get('nic_config_pb_loc')
58
59         kernel_image_settings = kwargs.get('kernel_image_settings')
60         if kernel_image_settings:
61             if isinstance(kernel_image_settings, dict):
62                 self.kernel_image_settings = ImageConfig(
63                     **kernel_image_settings)
64             else:
65                 self.kernel_image_settings = kernel_image_settings
66         else:
67             self.kernel_image_settings = None
68
69         ramdisk_image_settings = kwargs.get('ramdisk_image_settings')
70         if ramdisk_image_settings:
71             if isinstance(ramdisk_image_settings, dict):
72                 self.ramdisk_image_settings = ImageConfig(
73                     **ramdisk_image_settings)
74             else:
75                 self.ramdisk_image_settings = ramdisk_image_settings
76         else:
77             self.ramdisk_image_settings = None
78
79         if 'exists' in kwargs and kwargs['exists'] is True:
80             self.exists = True
81         else:
82             self.exists = False
83
84         if 'public' in kwargs and kwargs['public'] is True:
85             self.public = True
86         else:
87             self.public = False
88
89         if not self.name:
90             raise ImageConfigError("The attribute name is required")
91
92         if not (self.url or self.image_file) and not self.exists:
93             raise ImageConfigError(
94                 'URL or image file must be set or image must already exist')
95
96         if not self.image_user:
97             raise ImageConfigError('Image user is required')
98
99         if not self.format and not self.exists:
100             raise ImageConfigError(
101                 'Format is required when the image should not already exist')
102
103
104 class ImageConfigError(Exception):
105     """
106     Exception to be thrown when an image settings are incorrect
107     """
108
109     def __init__(self, message):
110         Exception.__init__(self, message)