Skip to content

Commit

Permalink
Enable Grafana mail notification (#5806)
Browse files Browse the repository at this point in the history
* update to new api-version

* code complete

* add sample

* code cleaning up
  • Loading branch information
yugangw-msft authored Jan 30, 2023
1 parent a12e79b commit df2474e
Show file tree
Hide file tree
Showing 37 changed files with 6,312 additions and 3,513 deletions.
3 changes: 3 additions & 0 deletions src/amg/azext_amg/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
- name: disable the public network access
text: |
az grafana update -g MyResourceGroup -n MyGrafana --public-network-access disabled
- name: enable mail notification through SMTP relay sevice of mailgun
text: |
az grafana update -g MyResourceGroup -n MyGrafana --smtp enabled --from-address johndoe@outlook.com --from-name john --host "smtp.mailgun.org:587" --user "postmaster@sandbox12345.mailgun.org" --password "password" --start-tls-policy OpportunisticStartTLS --skip-verify true
"""

helps['grafana data-source'] = """
Expand Down
8 changes: 8 additions & 0 deletions src/amg/azext_amg/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ def load_arguments(self, _):
help="if enabled, the Grafana workspace will have fixed egress IPs you can use them in the firewall of datasources")
c.argument("public_network_access", get_enum_type(["Enabled", "Disabled"]), options_list=["-p", "--public-network-access"],
help="allow public network access")
c.argument("smtp", get_enum_type(["Enabled", "Disabled"]), arg_group='SMTP', help="allow Grafana to send email")
c.argument("host", arg_group='SMTP', help="Smtp server url(port included)")
c.argument("user", arg_group='SMTP', help="Smtp server user name")
c.argument("password", arg_group='SMTP', help="Smtp server user password")
c.argument("from_address", arg_group='SMTP', help="Address used when sending out emails")
c.argument("from_name", arg_group='SMTP', help="Name to be used when sending out emails")
c.argument("start_tls_policy", get_enum_type(["OpportunisticStartTLS", "MandatoryStartTLS", "NoStartTLS"]), arg_group='SMTP', help="TLS policy")
c.argument("skip_verify", arg_group='SMTP', arg_type=get_three_state_flag(), help="Skip verifying SSL for SMTP server")

with self.argument_context("grafana dashboard") as c:
c.argument("uid", options_list=["--dashboard"], help="dashboard uid")
Expand Down
42 changes: 37 additions & 5 deletions src/amg/azext_amg/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,19 @@ def list_grafana(cmd, resource_group_name=None):


def update_grafana(cmd, grafana_name, api_key_and_service_account=None, deterministic_outbound_ip=None,
public_network_access=None, resource_group_name=None, tags=None):
if (not api_key_and_service_account and not deterministic_outbound_ip
and not public_network_access and not tags):
raise ArgumentUsageError("--api-key | --service-account | --tags"
"--deterministic-outbound-ip | --public-network-access")
public_network_access=None, smtp=None, host=None, user=None, password=None,
start_tls_policy=None, skip_verify=None, from_address=None, from_name=None,
resource_group_name=None, tags=None):

# pylint: disable=too-many-boolean-expressions, too-many-boolean-expressions

if (not api_key_and_service_account and not deterministic_outbound_ip and not public_network_access and not tags
and not smtp and not host and not user and not password and not start_tls_policy and not from_address
and not from_name and skip_verify is None):
raise ArgumentUsageError("Please supply at least one parameter value to update the Grafana workspace")

client = cf_amg(cmd.cli_ctx)

instance = client.grafana.get(resource_group_name, grafana_name)

if api_key_and_service_account:
Expand All @@ -170,6 +176,32 @@ def update_grafana(cmd, grafana_name, api_key_and_service_account=None, determin
if tags:
instance.tags = tags

if (smtp or host or user or password or start_tls_policy
or from_address or from_name or skip_verify is not None):

from azext_amg.vendored_sdks.models import GrafanaConfigurations, Smtp
if not instance.properties.grafana_configurations:
instance.properties.grafana_configurations = GrafanaConfigurations()
if not instance.properties.grafana_configurations.smtp:
instance.properties.grafana_configurations.smtp = Smtp()

if smtp:
instance.properties.grafana_configurations.smtp.enabled = (smtp == "Enabled")
if host:
instance.properties.grafana_configurations.smtp.host = host
if user:
instance.properties.grafana_configurations.smtp.user = user
if password:
instance.properties.grafana_configurations.smtp.password = password
if start_tls_policy:
instance.properties.grafana_configurations.smtp.start_tls_policy = start_tls_policy
if skip_verify is not None:
instance.properties.grafana_configurations.smtp.skip_verify = skip_verify
if from_address:
instance.properties.grafana_configurations.smtp.from_address = from_address
if from_name:
instance.properties.grafana_configurations.smtp.from_name = from_name

# "begin_create" uses PUT, which handles both Create and Update
return client.grafana.begin_create(resource_group_name, grafana_name, instance)

Expand Down
Loading

0 comments on commit df2474e

Please sign in to comment.