## root / snf-cyclades-app / synnefo / api / management / commands / modifynetwork.py @ af6a3bc5

History | View | Annotate | Download (4.1 kB)

1 | 8fad925a | Giorgos Verigakis | ```
# Copyright 2012 GRNET S.A. All rights reserved.
``` |
---|---|---|---|

2 | 8fad925a | Giorgos Verigakis | ```
#
``` |

3 | 8fad925a | Giorgos Verigakis | ```
# Redistribution and use in source and binary forms, with or
``` |

4 | 8fad925a | Giorgos Verigakis | ```
# without modification, are permitted provided that the following
``` |

5 | 8fad925a | Giorgos Verigakis | ```
# conditions are met:
``` |

6 | 8fad925a | Giorgos Verigakis | ```
#
``` |

7 | 8fad925a | Giorgos Verigakis | ```
# 1. Redistributions of source code must retain the above
``` |

8 | 8fad925a | Giorgos Verigakis | ```
# copyright notice, this list of conditions and the following
``` |

9 | 8fad925a | Giorgos Verigakis | ```
# disclaimer.
``` |

10 | 8fad925a | Giorgos Verigakis | ```
#
``` |

11 | 8fad925a | Giorgos Verigakis | ```
# 2. Redistributions in binary form must reproduce the above
``` |

12 | 8fad925a | Giorgos Verigakis | ```
# copyright notice, this list of conditions and the following
``` |

13 | 8fad925a | Giorgos Verigakis | ```
# disclaimer in the documentation and/or other materials
``` |

14 | 8fad925a | Giorgos Verigakis | ```
# provided with the distribution.
``` |

15 | 8fad925a | Giorgos Verigakis | ```
#
``` |

16 | 8fad925a | Giorgos Verigakis | ```
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
``` |

17 | 8fad925a | Giorgos Verigakis | ```
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
``` |

18 | 8fad925a | Giorgos Verigakis | ```
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
``` |

19 | 8fad925a | Giorgos Verigakis | ```
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
``` |

20 | 8fad925a | Giorgos Verigakis | ```
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
``` |

21 | 8fad925a | Giorgos Verigakis | ```
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
``` |

22 | 8fad925a | Giorgos Verigakis | ```
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
``` |

23 | 8fad925a | Giorgos Verigakis | ```
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
``` |

24 | 8fad925a | Giorgos Verigakis | ```
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
``` |

25 | 8fad925a | Giorgos Verigakis | ```
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
``` |

26 | 8fad925a | Giorgos Verigakis | ```
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
``` |

27 | 8fad925a | Giorgos Verigakis | ```
# POSSIBILITY OF SUCH DAMAGE.
``` |

28 | 8fad925a | Giorgos Verigakis | ```
#
``` |

29 | 8fad925a | Giorgos Verigakis | ```
# The views and conclusions contained in the software and
``` |

30 | 8fad925a | Giorgos Verigakis | ```
# documentation are those of the authors and should not be
``` |

31 | 8fad925a | Giorgos Verigakis | ```
# interpreted as representing official policies, either expressed
``` |

32 | 8fad925a | Giorgos Verigakis | ```
# or implied, of GRNET S.A.
``` |

33 | 8fad925a | Giorgos Verigakis | |

34 | 8fad925a | Giorgos Verigakis | from optparse import make_option |

35 | 8fad925a | Giorgos Verigakis | |

36 | 8fad925a | Giorgos Verigakis | from django.core.management.base import BaseCommand, CommandError |

37 | 8fad925a | Giorgos Verigakis | |

38 | 8fad925a | Giorgos Verigakis | from synnefo.db.models import Network |

39 | 8fad925a | Giorgos Verigakis | |

40 | 8fad925a | Giorgos Verigakis | |

41 | 8fad925a | Giorgos Verigakis | class Command(BaseCommand): |

42 | 8fad925a | Giorgos Verigakis | ```
args = "<network id>"
``` |

43 | 8fad925a | Giorgos Verigakis | ```
help = "Modify a network"
``` |

44 | 8fad925a | Giorgos Verigakis | |

45 | 8fad925a | Giorgos Verigakis | option_list = BaseCommand.option_list + ( |

46 | 8fad925a | Giorgos Verigakis | ```
make_option('--name',
``` |

47 | 8fad925a | Giorgos Verigakis | ```
dest='name',
``` |

48 | 8fad925a | Giorgos Verigakis | ```
metavar='NAME',
``` |

49 | 8fad925a | Giorgos Verigakis | ```
help="Set network's name"),
``` |

50 | 8fad925a | Giorgos Verigakis | ```
make_option('--owner',
``` |

51 | 8fad925a | Giorgos Verigakis | ```
dest='owner',
``` |

52 | 8fad925a | Giorgos Verigakis | ```
metavar='USER_ID',
``` |

53 | 8fad925a | Giorgos Verigakis | ```
help="Set network's owner"),
``` |

54 | bf1d3a70 | Christos Stavrakakis | ```
make_option('--subnet',
``` |

55 | bf1d3a70 | Christos Stavrakakis | ```
dest='subnet',
``` |

56 | bf1d3a70 | Christos Stavrakakis | ```
help="Set network's subnet"),
``` |

57 | bf1d3a70 | Christos Stavrakakis | ```
make_option('--gateway',
``` |

58 | bf1d3a70 | Christos Stavrakakis | ```
dest='gateway',
``` |

59 | bf1d3a70 | Christos Stavrakakis | ```
help="Set network's gateway"),
``` |

60 | cc3f266e | Christos Stavrakakis | ```
make_option('--subnet6',
``` |

61 | cc3f266e | Christos Stavrakakis | ```
dest='subnet6',
``` |

62 | cc3f266e | Christos Stavrakakis | ```
help="Set network's IPv6 subnet"),
``` |

63 | cc3f266e | Christos Stavrakakis | ```
make_option('--gateway6',
``` |

64 | cc3f266e | Christos Stavrakakis | ```
dest='gateway6',
``` |

65 | cc3f266e | Christos Stavrakakis | ```
help="Set network's IPv6 gateway"),
``` |

66 | bf1d3a70 | Christos Stavrakakis | ```
make_option('--dhcp',
``` |

67 | bf1d3a70 | Christos Stavrakakis | ```
dest='dhcp',
``` |

68 | bf1d3a70 | Christos Stavrakakis | ```
help="Set if network will use nfdhcp"),
``` |

69 | 8fad925a | Giorgos Verigakis | ```
make_option('--state',
``` |

70 | 8fad925a | Giorgos Verigakis | ```
dest='state',
``` |

71 | 8fad925a | Giorgos Verigakis | ```
metavar='STATE',
``` |

72 | 8fad925a | Giorgos Verigakis | ```
help="Set network's state")
``` |

73 | 8fad925a | Giorgos Verigakis | ) |

74 | 8fad925a | Giorgos Verigakis | |

75 | 8fad925a | Giorgos Verigakis | def handle(self, *args, **options): |

76 | 8fad925a | Giorgos Verigakis | if len(args) != 1: |

77 | 8fad925a | Giorgos Verigakis | raise CommandError("Please provide a server ID") |

78 | 8fad925a | Giorgos Verigakis | |

79 | 8fad925a | Giorgos Verigakis | ```
try:
``` |

80 | 8fad925a | Giorgos Verigakis | network_id = int(args[0]) |

81 | 8fad925a | Giorgos Verigakis | network = Network.objects.get(id=network_id) |

82 | 8fad925a | Giorgos Verigakis | except (ValueError, Network.DoesNotExist): |

83 | 8fad925a | Giorgos Verigakis | raise CommandError("Invalid network id") |

84 | 8fad925a | Giorgos Verigakis | |

85 | 8fad925a | Giorgos Verigakis | ```
name = options.get('name')
``` |

86 | 8fad925a | Giorgos Verigakis | if name is not None: |

87 | 8fad925a | Giorgos Verigakis | network.name = name |

88 | 8fad925a | Giorgos Verigakis | |

89 | 8fad925a | Giorgos Verigakis | ```
owner = options.get('owner')
``` |

90 | 8fad925a | Giorgos Verigakis | if owner is not None: |

91 | 8fad925a | Giorgos Verigakis | network.userid = owner |

92 | 8fad925a | Giorgos Verigakis | |

93 | bf1d3a70 | Christos Stavrakakis | ```
subnet = options.get('subnet')
``` |

94 | bf1d3a70 | Christos Stavrakakis | if subnet is not None: |

95 | bf1d3a70 | Christos Stavrakakis | network.subnet = subnet |

96 | bf1d3a70 | Christos Stavrakakis | |

97 | bf1d3a70 | Christos Stavrakakis | ```
gateway = options.get('gateway')
``` |

98 | bf1d3a70 | Christos Stavrakakis | if gateway is not None: |

99 | bf1d3a70 | Christos Stavrakakis | network.gateway = gateway |

100 | bf1d3a70 | Christos Stavrakakis | |

101 | cc3f266e | Christos Stavrakakis | ```
subnet6 = options.get('subnet6')
``` |

102 | cc3f266e | Christos Stavrakakis | if subnet6 is not None: |

103 | cc3f266e | Christos Stavrakakis | network.subnet6 = subnet6 |

104 | cc3f266e | Christos Stavrakakis | |

105 | cc3f266e | Christos Stavrakakis | ```
gateway6 = options.get('gateway6')
``` |

106 | cc3f266e | Christos Stavrakakis | if gateway6 is not None: |

107 | cc3f266e | Christos Stavrakakis | network.gateway6 = gateway6 |

108 | cc3f266e | Christos Stavrakakis | |

109 | bf1d3a70 | Christos Stavrakakis | ```
dhcp = options.get('dhcp')
``` |

110 | bf1d3a70 | Christos Stavrakakis | if dhcp is not None: |

111 | bf1d3a70 | Christos Stavrakakis | network.dhcp = dhcp |

112 | bf1d3a70 | Christos Stavrakakis | |

113 | 8fad925a | Giorgos Verigakis | ```
state = options.get('state')
``` |

114 | 8fad925a | Giorgos Verigakis | if state is not None: |

115 | fd33b074 | Kostas Papadimitriou | allowed = [x[0] for x in Network.OPER_STATES] |

116 | 8fad925a | Giorgos Verigakis | if state not in allowed: |

117 | 8fad925a | Giorgos Verigakis | msg = "Invalid state, must be one of %s" % ', '.join(allowed) |

118 | 8fad925a | Giorgos Verigakis | ```
raise CommandError(msg)
``` |

119 | 8fad925a | Giorgos Verigakis | network.state = state |

120 | 8fad925a | Giorgos Verigakis | |

121 | 8fad925a | Giorgos Verigakis | network.save() |