Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

# coding: utf-8 

# 

# Copyright 2013 Red Hat, Inc. 

# 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. 

 

import netaddr 

from oslo_utils import strutils 

import six 

import wsme 

from wsme import types as wtypes 

 

from magnum.common import exception 

from magnum.common import utils 

from magnum.openstack.common._i18n import _ 

 

 

class MacAddressType(wtypes.UserType): 

    """A simple MAC address type.""" 

 

    basetype = wtypes.text 

    name = 'macaddress' 

    # FIXME(lucasagomes): When used with wsexpose decorator WSME will try 

    # to get the name of the type by accessing it's __name__ attribute. 

    # Remove this __name__ attribute once it's fixed in WSME. 

    # https://bugs.launchpad.net/wsme/+bug/1265590 

    __name__ = name 

 

    @staticmethod 

    def validate(value): 

        return utils.validate_and_normalize_mac(value) 

 

    @staticmethod 

    def frombasetype(value): 

        if value is None: 

            return None 

        return MacAddressType.validate(value) 

 

 

class NameType(wtypes.UserType): 

    """A logical name type.""" 

 

    basetype = wtypes.text 

    name = 'name' 

    # FIXME(lucasagomes): When used with wsexpose decorator WSME will try 

    # to get the name of the type by accessing it's __name__ attribute. 

    # Remove this __name__ attribute once it's fixed in WSME. 

    # https://bugs.launchpad.net/wsme/+bug/1265590 

    __name__ = name 

 

    @staticmethod 

    def validate(value): 

        if not utils.is_name_safe(value): 

            raise exception.InvalidName(name=value) 

        return value 

 

    @staticmethod 

    def frombasetype(value): 

        if value is None: 

            return None 

        return NameType.validate(value) 

 

 

class UuidType(wtypes.UserType): 

    """A simple UUID type.""" 

 

    basetype = wtypes.text 

    name = 'uuid' 

    # FIXME(lucasagomes): When used with wsexpose decorator WSME will try 

    # to get the name of the type by accessing it's __name__ attribute. 

    # Remove this __name__ attribute once it's fixed in WSME. 

    # https://bugs.launchpad.net/wsme/+bug/1265590 

    __name__ = name 

 

    @staticmethod 

    def validate(value): 

        if not utils.is_uuid_like(value): 

            raise exception.InvalidUUID(uuid=value) 

        return value 

 

    @staticmethod 

    def frombasetype(value): 

        if value is None: 

            return None 

        return UuidType.validate(value) 

 

 

class BooleanType(wtypes.UserType): 

    """A simple boolean type.""" 

 

    basetype = wtypes.text 

    name = 'boolean' 

    # FIXME(lucasagomes): When used with wsexpose decorator WSME will try 

    # to get the name of the type by accessing it's __name__ attribute. 

    # Remove this __name__ attribute once it's fixed in WSME. 

    # https://bugs.launchpad.net/wsme/+bug/1265590 

    __name__ = name 

 

    @staticmethod 

    def validate(value): 

        try: 

            return strutils.bool_from_string(value, strict=True) 

        except ValueError as e: 

            # raise Invalid to return 400 (BadRequest) in the API 

            raise exception.Invalid(e) 

 

    @staticmethod 

    def frombasetype(value): 

        if value is None: 

            return None 

        return BooleanType.validate(value) 

 

 

class MultiType(wtypes.UserType): 

    """A complex type that represents one or more types. 

 

    Used for validating that a value is an instance of one of the types. 

 

    :param types: Variable-length list of types. 

 

    """ 

    basetype = wtypes.text 

 

    def __init__(self, *types): 

        self.types = types 

 

    def __str__(self): 

        return ' | '.join(map(str, self.types)) 

 

    def validate(self, value): 

        for t in self.types: 

            try: 

                return wtypes.validate_value(t, value) 

            except (exception.InvalidUUID, ValueError): 

                pass 

        else: 

            raise ValueError( 

                     _("Wrong type. Expected '%(type)s', got '%(value)s'") 

                     % {'type': self.types, 'value': type(value)}) 

 

 

macaddress = MacAddressType() 

uuid = UuidType() 

name = NameType() 

uuid_or_name = MultiType(UuidType, NameType) 

boolean = BooleanType() 

 

 

class JsonPatchType(wtypes.Base): 

    """A complex type that represents a single json-patch operation.""" 

 

    path = wtypes.wsattr(wtypes.StringType(pattern='^(/[\w-]+)+$'), 

                         mandatory=True) 

    op = wtypes.wsattr(wtypes.Enum(str, 'add', 'replace', 'remove'), 

                       mandatory=True) 

    value = MultiType(wtypes.text, int) 

 

    @staticmethod 

    def internal_attrs(): 

        """Returns a list of internal attributes. 

 

        Internal attributes can't be added, replaced or removed. This 

        method may be overwritten by derived class. 

 

        """ 

        return ['/created_at', '/id', '/links', '/updated_at', '/uuid'] 

 

    @staticmethod 

    def mandatory_attrs(): 

        """Retruns a list of mandatory attributes. 

 

        Mandatory attributes can't be removed from the document. This 

        method should be overwritten by derived class. 

 

        """ 

        return [] 

 

    @staticmethod 

    def validate(patch): 

        if patch.path in patch.internal_attrs(): 

            msg = _("'%s' is an internal attribute and can not be updated") 

            raise wsme.exc.ClientSideError(msg % patch.path) 

 

        if patch.path in patch.mandatory_attrs() and patch.op == 'remove': 

            msg = _("'%s' is a mandatory attribute and can not be removed") 

            raise wsme.exc.ClientSideError(msg % patch.path) 

 

        if patch.op != 'remove': 

            if not patch.value: 

                msg = _("'add' and 'replace' operations needs value") 

                raise wsme.exc.ClientSideError(msg) 

 

        ret = {'path': patch.path, 'op': patch.op} 

        if patch.value: 

            ret['value'] = patch.value 

        return ret 

 

 

class IPv4AddressType(wtypes.UserType): 

    """A simple IPv4 type.""" 

 

    basetype = six.string_types 

    name = "ipv4address" 

 

    @staticmethod 

    def validate(value): 

        try: 

            netaddr.IPAddress(value, version=4, flags=netaddr.INET_PTON) 

        except netaddr.AddrFormatError as e: 

            raise ValueError(six.text_type(e))