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
6# pylint: disable=line-too-long
7# pylint: disable=too-many-lines
8# pylint: disable=inconsistent-return-statements
9# pylint: disable=unused-variable
10
11
12# Namespace Region
13def cli_namespace_create(client, resource_group_name, namespace_name, location=None, tags=None):
14    from azure.mgmt.relay.models import RelayNamespace
15    return client.create_or_update(
16        resource_group_name=resource_group_name,
17        namespace_name=namespace_name,
18        parameters=RelayNamespace(
19            location,
20            tags)
21    )
22
23
24def cli_namespace_update(instance, tags=None):
25
26    if tags is not None:
27        instance.tags = tags
28
29    return instance
30
31
32def cli_namespace_list(client, resource_group_name=None):
33    if resource_group_name:
34        return client.list_by_resource_group(resource_group_name=resource_group_name)
35
36    return client.list()
37
38
39# Namespace Authorization rule:
40def cli_namespaceautho_create(client, resource_group_name, namespace_name, name, access_rights=None):
41    from azure.cli.command_modules.relay._utils import accessrights_converter
42    return client.create_or_update_authorization_rule(
43        resource_group_name=resource_group_name,
44        namespace_name=namespace_name,
45        authorization_rule_name=name,
46        rights=accessrights_converter(access_rights)
47    )
48
49
50# Namespace Authorization rule:
51def cli_namespaceautho_update(instance, rights):
52    from azure.cli.command_modules.relay._utils import accessrights_converter
53    instance.rights = accessrights_converter(rights)
54    return instance
55
56
57# WCF Relay Region
58def cli_wcfrelay_create(client, resource_group_name, namespace_name, relay_name, relay_type,
59                        requires_client_authorization=None, requires_transport_security=None, user_metadata=None):
60
61    from azure.mgmt.relay.models import WcfRelay, Relaytype
62
63    if relay_type is None:
64        set_relay_type = Relaytype.net_tcp
65    elif relay_type == "Http":
66        set_relay_type = Relaytype.http
67    else:
68        set_relay_type = Relaytype.net_tcp
69
70    wcfrelay_params = WcfRelay(
71        relay_type=set_relay_type,
72        requires_client_authorization=requires_client_authorization,
73        requires_transport_security=requires_transport_security,
74        user_metadata=user_metadata
75    )
76
77    return client.create_or_update(
78        resource_group_name=resource_group_name,
79        namespace_name=namespace_name,
80        relay_name=relay_name,
81        parameters=wcfrelay_params)
82
83
84def cli_wcfrelay_update(instance, relay_type=None, user_metadata=None, status=None):
85
86    from azure.mgmt.relay.models import WcfRelay
87    returnobj = WcfRelay(relay_type=instance.relay_type,
88                         requires_client_authorization=instance.requires_client_authorization,
89                         requires_transport_security=instance.requires_transport_security,
90                         user_metadata=instance.user_metadata)
91
92    if relay_type:
93        returnobj.relay_type = relay_type
94
95    if user_metadata:
96        returnobj.user_metadata = user_metadata
97
98    if status:
99        returnobj.status = status
100
101    return returnobj
102
103
104# Hybrid Connection Region
105def cli_hyco_create(client, resource_group_name, namespace_name, hybrid_connection_name,
106                    requires_client_authorization=None, user_metadata=None):
107
108    return client.create_or_update(
109        resource_group_name=resource_group_name,
110        namespace_name=namespace_name,
111        hybrid_connection_name=hybrid_connection_name,
112        requires_client_authorization=requires_client_authorization, user_metadata=user_metadata)
113
114
115def cli_hyco_update(instance, requires_client_authorization=None, status=None, user_metadata=None):
116
117    from azure.mgmt.relay.models import HybridConnection
118    hyco_params = HybridConnection(requires_client_authorization=instance.requires_client_authorization,
119                                   user_metadata=instance.user_metadata)
120
121    if requires_client_authorization:
122        hyco_params.requires_client_authorization = requires_client_authorization
123
124    if status:
125        hyco_params.status = status
126
127    if user_metadata:
128        hyco_params.user_metadata = user_metadata
129
130    return hyco_params
131
132
133def empty_on_404(ex):
134    from azure.mgmt.relay.models import ErrorResponseException
135    if isinstance(ex, ErrorResponseException) and ex.response.status_code == 404:
136        return None
137    raise ex
138