|
| 1 | +import json |
| 2 | +import logging |
| 3 | +from typing import Union |
| 4 | + |
| 5 | +from .model import ( |
| 6 | + APIModel, |
| 7 | + APIEndpoints, |
| 8 | + RequestsMethods, |
| 9 | + SSOSetting, |
| 10 | +) |
| 11 | +from .api import Api |
| 12 | + |
| 13 | + |
| 14 | +class SSOSettings: |
| 15 | + """The class includes all necessary methods to access the Grafana sso settings API endpoints |
| 16 | +
|
| 17 | + Args: |
| 18 | + grafana_api_model (APIModel): Inject a Grafana API model object that includes all necessary values and information |
| 19 | +
|
| 20 | + Attributes: |
| 21 | + grafana_api_model (APIModel): This is where we store the grafana_api_model |
| 22 | + """ |
| 23 | + |
| 24 | + def __init__(self, grafana_api_model: APIModel): |
| 25 | + self.grafana_api_model = grafana_api_model |
| 26 | + |
| 27 | + def get_sso_settings(self) -> list: |
| 28 | + """The method includes a functionality to get the SSO settings for all providers |
| 29 | +
|
| 30 | + Required Permissions: |
| 31 | + Action: settings:read |
| 32 | + Scope: settings:auth.{provider}:* |
| 33 | +
|
| 34 | + Raises: |
| 35 | + Exception: Unspecified error by executing the API call |
| 36 | +
|
| 37 | + Returns: |
| 38 | + api_call (list): Returns the all SSO settings |
| 39 | + """ |
| 40 | + |
| 41 | + api_call: Union[list, dict] = Api(self.grafana_api_model).call_the_api( |
| 42 | + f"{APIEndpoints.SSO_SETTINGS.value}", |
| 43 | + response_status_code=True, |
| 44 | + ) |
| 45 | + |
| 46 | + status_code: int = api_call[0].get("status") if isinstance(api_call, list) else api_call.get("status") |
| 47 | + |
| 48 | + sso_settings_status_dict: dict = dict( |
| 49 | + { |
| 50 | + 400: "Bad request.", |
| 51 | + 401: "Unauthorized.", |
| 52 | + 403: "Access Denied.", |
| 53 | + } |
| 54 | + ) |
| 55 | + |
| 56 | + if status_code == 200: |
| 57 | + return api_call |
| 58 | + elif 400 <= status_code <= 403: |
| 59 | + logging.error(sso_settings_status_dict.get(status_code)) |
| 60 | + raise Exception |
| 61 | + else: |
| 62 | + logging.error(f"Check the error: {api_call}.") |
| 63 | + raise Exception |
| 64 | + |
| 65 | + def get_sso_settings_by_provider(self, provider: str) -> dict: |
| 66 | + """The method includes a functionality to get the SSO settings for the specified provider |
| 67 | +
|
| 68 | + Args: |
| 69 | + provider (str): Specify the provider |
| 70 | +
|
| 71 | + Required Permissions: |
| 72 | + Action: settings:read |
| 73 | + Scope: settings:auth.{provider}:* |
| 74 | +
|
| 75 | + Raises: |
| 76 | + ValueError: Missed specifying a necessary value |
| 77 | + Exception: Unspecified error by executing the API call |
| 78 | +
|
| 79 | + Returns: |
| 80 | + api_call (dict): Returns the corresponding provider SSO settings |
| 81 | + """ |
| 82 | + |
| 83 | + if len(provider) != 0: |
| 84 | + api_call: dict = Api(self.grafana_api_model).call_the_api( |
| 85 | + f"{APIEndpoints.SSO_SETTINGS.value}/{provider}", |
| 86 | + response_status_code=True |
| 87 | + ) |
| 88 | + |
| 89 | + status_code: int = api_call.get("status") |
| 90 | + |
| 91 | + sso_settings_status_dict: dict = dict( |
| 92 | + { |
| 93 | + 400: "Bad request.", |
| 94 | + 401: "Unauthorized.", |
| 95 | + 403: "Access Denied.", |
| 96 | + 404: "SSO Settings not found." |
| 97 | + } |
| 98 | + ) |
| 99 | + |
| 100 | + if status_code == 200: |
| 101 | + return api_call |
| 102 | + elif 400 <= status_code <= 404: |
| 103 | + logging.error(sso_settings_status_dict.get(status_code)) |
| 104 | + raise Exception |
| 105 | + else: |
| 106 | + logging.error(f"Check the error: {api_call}.") |
| 107 | + raise Exception |
| 108 | + else: |
| 109 | + logging.error("There is no provider defined.") |
| 110 | + raise ValueError |
| 111 | + |
| 112 | + def update_sso_settings(self, provider: str, sso_setting: SSOSetting): |
| 113 | + """The method includes a functionality to update the SSO settings specified by the provider |
| 114 | +
|
| 115 | + Args: |
| 116 | + provider (str): Specify the provider |
| 117 | + sso_setting (SSOSetting): Specify the SSO setting |
| 118 | +
|
| 119 | + Required Permissions: |
| 120 | + Action: settings:write |
| 121 | + Scope: settings:auth.{provider}:* |
| 122 | +
|
| 123 | + Raises: |
| 124 | + ValueError: Missed specifying a necessary value |
| 125 | + Exception: Unspecified error by executing the API call |
| 126 | +
|
| 127 | + Returns: |
| 128 | + None |
| 129 | + """ |
| 130 | + |
| 131 | + if len(provider) != 0 and sso_setting is not None and len(sso_setting.api_url) != 0 and len(sso_setting.client_id) != 0 and len(sso_setting.client_secret) != 0 and sso_setting.enabled is not None and len(sso_setting.scopes) != 0: |
| 132 | + api_call: dict = Api(self.grafana_api_model).call_the_api( |
| 133 | + f"{APIEndpoints.SSO_SETTINGS.value}/{provider}", |
| 134 | + RequestsMethods.PUT, |
| 135 | + json.dumps( |
| 136 | + dict( |
| 137 | + { |
| 138 | + "settings": { |
| 139 | + "apiUrl": sso_setting.api_url, |
| 140 | + "clientId": sso_setting.client_id, |
| 141 | + "clientSecret": sso_setting.client_secret, |
| 142 | + "enabled": sso_setting.enabled, |
| 143 | + "scopes": sso_setting.scopes, |
| 144 | + } |
| 145 | + } |
| 146 | + ) |
| 147 | + ), |
| 148 | + response_status_code=True, |
| 149 | + ) |
| 150 | + |
| 151 | + status_code: int = api_call.get("status") |
| 152 | + |
| 153 | + sso_settings_status_dict: dict = dict( |
| 154 | + { |
| 155 | + 400: "Bad request.", |
| 156 | + 401: "Unauthorized.", |
| 157 | + 403: "Access Denied.", |
| 158 | + } |
| 159 | + ) |
| 160 | + |
| 161 | + if status_code == 204: |
| 162 | + logging.info("You successfully updated the corresponding provider SSO setting.") |
| 163 | + elif 400 <= status_code <= 403: |
| 164 | + logging.error(sso_settings_status_dict.get(status_code)) |
| 165 | + raise Exception |
| 166 | + else: |
| 167 | + logging.error(f"Check the error: {api_call}.") |
| 168 | + raise Exception |
| 169 | + else: |
| 170 | + logging.error("There is no provider or sso_settings object defined.") |
| 171 | + raise ValueError |
| 172 | + |
| 173 | + def delete_sso_settings(self, provider: str): |
| 174 | + """The method includes a functionality to delete the SSO settings specified by the provider |
| 175 | +
|
| 176 | + Args: |
| 177 | + provider (str): Specify the provider |
| 178 | +
|
| 179 | + Required Permissions: |
| 180 | + Action: settings:write |
| 181 | + Scope: settings:auth.{provider}:* |
| 182 | +
|
| 183 | + Raises: |
| 184 | + ValueError: Missed specifying a necessary value |
| 185 | + Exception: Unspecified error by executing the API call |
| 186 | +
|
| 187 | + Returns: |
| 188 | + None |
| 189 | + """ |
| 190 | + |
| 191 | + if len(provider) != 0: |
| 192 | + api_call: dict = Api(self.grafana_api_model).call_the_api( |
| 193 | + f"{APIEndpoints.SSO_SETTINGS.value}/{provider}", |
| 194 | + RequestsMethods.DELETE, |
| 195 | + response_status_code=True, |
| 196 | + ) |
| 197 | + |
| 198 | + status_code: int = api_call.get("status") |
| 199 | + |
| 200 | + sso_settings_status_dict: dict = dict( |
| 201 | + { |
| 202 | + 400: "Bad request.", |
| 203 | + 401: "Unauthorized.", |
| 204 | + 403: "Access Denied.", |
| 205 | + 404: "SSO Settings not found.", |
| 206 | + } |
| 207 | + ) |
| 208 | + |
| 209 | + if status_code == 204: |
| 210 | + logging.info("You successfully deleted the corresponding provider SSO settings.") |
| 211 | + elif 400 <= status_code <= 404: |
| 212 | + logging.error(sso_settings_status_dict.get(status_code)) |
| 213 | + raise Exception |
| 214 | + else: |
| 215 | + logging.error(f"Check the error: {api_call}.") |
| 216 | + raise Exception |
| 217 | + else: |
| 218 | + logging.error("There is no provider defined.") |
| 219 | + raise ValueError |
0 commit comments