1# --------------------------------------------------------------------------------------------
2# Copyright (c) Microsoft Corporation. All rights reserved.
3# Licensed under the MIT License. See License.txt in the project root for license information.
4# --------------------------------------------------------------------------------------------
5
6import argparse
7
8from knack.util import CLIError
9
10
11# pylint:disable=protected-access
12# pylint:disable=too-few-public-methods
13class OriginType(argparse._AppendAction):
14    def __call__(self, parser, namespace, values, option_string=None):
15        deep_created_origin = self.get_origin(values, option_string)
16        super(OriginType, self).__call__(parser, namespace, deep_created_origin, option_string)
17
18    def get_origin(self, values, option_string):
19        from azure.mgmt.cdn.models import DeepCreatedOrigin
20
21        if not 1 <= len(values) <= 3 and not 5 <= len(values) <= 6:
22            msg = '%s takes 1, 2, 3, 5, or 6 values, %d given'
23            raise argparse.ArgumentError(
24                self, msg % (option_string, len(values)))
25
26        deep_created_origin = DeepCreatedOrigin(
27            name='origin',
28            host_name=values[0],
29            http_port=80,
30            https_port=443)
31
32        if len(values) > 1:
33            deep_created_origin.http_port = int(values[1])
34        if len(values) > 2:
35            deep_created_origin.https_port = int(values[2])
36        if len(values) > 4:
37            deep_created_origin.private_link_resource_id = values[3]
38            deep_created_origin.private_link_location = values[4]
39        if len(values) > 5:
40            deep_created_origin.private_link_approval_message = values[5]
41        return deep_created_origin
42
43
44# pylint: disable=protected-access
45# pylint:disable=too-few-public-methods
46class MatchConditionAction(argparse._AppendAction):
47
48    def __call__(self, parser, namespace, values, option_string=None):
49        match_condition = get_match_condition(values, option_string)
50        super(MatchConditionAction, self).__call__(parser, namespace, match_condition, option_string)
51
52
53def get_match_condition(values, option_string):
54    from azure.mgmt.cdn.models import MatchCondition
55
56    match_values = []
57    match_variable = None
58    negate_condition = None
59    operator = None
60    selector = None
61    transforms = []
62    for item in values:
63        if '=' not in item:
64            raise CLIError('usage error: {} KEY=VALUE [KEY=VALUE ...]'.format(option_string))
65        key, value = item.split('=', 1)
66        key = key.lower()
67
68        if key == "match-value":
69            match_values.append(value)
70        elif key == "transform":
71            transforms.append(value)
72        elif key == "match-variable":
73            if match_variable is not None:
74                raise CLIError('usage error: match-variable may only be specified once per match condition.')
75            match_variable = value
76        elif key == "negate":
77            if negate_condition is not None:
78                raise CLIError('usage error: negate may only be specified once per match condition.')
79            negate_condition = value.lower() == "true"
80        elif key == "operator":
81            if operator is not None:
82                raise CLIError('usage error: operator may only be specified once per match condition.')
83            operator = value
84        elif key == "selector":
85            if selector is not None:
86                raise CLIError('usage error: selector may only be specified once per match condition.')
87            selector = value
88        else:
89            raise CLIError('usage error: unrecognized key {}'.format(key))
90    return MatchCondition(match_variable=match_variable,
91                          match_value=match_values,
92                          negate_condition=negate_condition,
93                          operator=operator,
94                          selector=selector,
95                          transforms=transforms)
96
97
98# pylint: disable=protected-access
99# pylint:disable=too-few-public-methods
100class ManagedRuleOverrideAction(argparse._AppendAction):
101
102    def __call__(self, parser, namespace, values, option_string=None):
103        rule_override = get_rule_override(values, option_string)
104        super(ManagedRuleOverrideAction, self).__call__(parser, namespace, rule_override, option_string)
105
106
107def get_rule_override(values, option_string):
108    from azure.mgmt.cdn.models import ManagedRuleOverride
109
110    rule_id = None
111    action = None
112    enabled = None
113
114    for item in values:
115        if '=' not in item:
116            raise CLIError('usage error: {} KEY=VALUE [KEY=VALUE ...]'.format(option_string))
117        key, value = item.split('=', 1)
118        key = key.lower()
119
120        if key == "id":
121            if rule_id is not None:
122                raise CLIError('usage error: id may only be specified once per rule override.')
123            rule_id = value
124        elif key == "action":
125            if action is not None:
126                raise CLIError('usage error: action may only be specified once per rule override.')
127            action = value
128        elif key == "enabled":
129            if enabled is not None:
130                raise CLIError('usage error: enabled may only be specified once per rule override.')
131            enabled = value
132        else:
133            raise CLIError('usage error: unrecognized key {}'.format(key))
134    return ManagedRuleOverride(rule_id=rule_id,
135                               action=action,
136                               enabled_state=enabled)
137