Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

credential operations #18925

Merged
merged 16 commits into from
May 26, 2021
3 changes: 2 additions & 1 deletion sdk/metricsadvisor/azure-ai-metricsadvisor/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Release History

## 1.0.0b4 (Unreleased)
## 1.0.0 (2021-06-07)

**New Features**
- Added AzureLogAnalyticsDataFeedSource and AzureEventHubsDataFeedSource
- Update method now returns the updated object
- Added CredentialEntities and CredentialEntity operations

**Breaking Changes**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
ChangePointFeedback,
CommentFeedback,
PeriodFeedback,
DataFeedRollupType
DataFeedRollupType,
SQLConnectionStringCredentialEntity,
DataLakeGen2SharedKeyCredentialEntity,
ServicePrincipalCredentialEntity,
ServicePrincipalInKVCredentialEntity
)
from ._metrics_advisor_key_credential import MetricsAdvisorKeyCredential
from ._metrics_advisor_key_credential_policy import MetricsAdvisorKeyCredentialPolicy
Expand Down Expand Up @@ -148,7 +152,7 @@ def convert_to_generated_data_feed_type(
)

return generated_feed_type(
data_source_parameter=source.__dict__,
data_source_parameter=source._to_generated(),
data_feed_name=name,
granularity_name=granularity.granularity_type,
granularity_amount=granularity.custom_granularity_value,
Expand Down Expand Up @@ -217,3 +221,12 @@ def get_authentication_policy(credential):
)

return authentication_policy

def convert_to_credential_entity(credential_entity):
if credential_entity.data_source_credential_type == "AzureSQLConnectionString":
return SQLConnectionStringCredentialEntity._from_generated(credential_entity)
if credential_entity.data_source_credential_type == "DataLakeGen2SharedKey":
return DataLakeGen2SharedKeyCredentialEntity._from_generated(credential_entity)
if credential_entity.data_source_credential_type == "ServicePrincipal":
return ServicePrincipalCredentialEntity._from_generated(credential_entity)
return ServicePrincipalInKVCredentialEntity._from_generated(credential_entity)
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
construct_data_feed_dict,
convert_datetime,
get_authentication_policy,
convert_to_credential_entity,
)
from .models._models import (
DataFeed,
Expand All @@ -81,6 +82,10 @@
MongoDBDataFeedSource,
AzureDataLakeStorageGen2DataFeedSource,
AzureEventHubsDataFeedSource,
SQLConnectionStringCredentialEntity,
DataLakeGen2SharedKeyCredentialEntity,
ServicePrincipalCredentialEntity,
ServicePrincipalInKVCredentialEntity
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -113,6 +118,13 @@
AzureEventHubsDataFeedSource,
]

CredentialEntityUnion = Union[
SQLConnectionStringCredentialEntity,
DataLakeGen2SharedKeyCredentialEntity,
ServicePrincipalCredentialEntity,
ServicePrincipalInKVCredentialEntity,
]

DATA_FEED = {
"SqlServer": _SQLServerDataFeed,
"AzureApplicationInsights": _AzureApplicationInsightsDataFeed,
Expand Down Expand Up @@ -1217,3 +1229,175 @@ def list_data_feed_ingestion_status(
skip=skip,
**kwargs
)

@distributed_trace
def get_credential_entity(
self,
credential_entity_id, # type: str
**kwargs # type: Any
):
xiangyan99 marked this conversation as resolved.
Show resolved Hide resolved
# type: (...) -> CredentialEntityUnion
"""Get a data source credential entity
:param credential_entity_id: Data source credential entity unique ID.
:type credential_entity_id: str
:return: The credential entity
:rtype: Union[~azure.ai.metricsadvisor.models.SQLConnectionStringCredentialEntity,
~azure.ai.metricsadvisor.models.DataLakeGen2SharedKeyCredentialEntity,
~azure.ai.metricsadvisor.models.ServicePrincipalCredentialEntity,
~azure.ai.metricsadvisor.models.ServicePrincipalInKVCredentialEntity]
:raises ~azure.core.exceptions.HttpResponseError:
.. admonition:: Example:
.. literalinclude:: ../samples/sample_credential_entities.py
:start-after: [START get_credential_entity]
:end-before: [END get_credential_entity]
:language: python
:dedent: 4
:caption: Get a credential entity by its ID
"""

credential_entity = self._client.get_credential(credential_entity_id, **kwargs)
return convert_to_credential_entity(credential_entity)

@distributed_trace
def create_credential_entity(
self, credential_entity, # type: CredentialEntityUnion
**kwargs # type: Any
):
# type: (...) -> CredentialEntityUnion
"""Create a new data source credential entity.
:param credential_entity: The data source credential entity to create
:type credential_entity: Union[~azure.ai.metricsadvisor.models.SQLConnectionStringCredentialEntity,
~azure.ai.metricsadvisor.models.DataLakeGen2SharedKeyCredentialEntity,
~azure.ai.metricsadvisor.models.ServicePrincipalCredentialEntity,
~azure.ai.metricsadvisor.models.ServicePrincipalInKVCredentialEntity]
:return: The created data source credential entity
:rtype: Union[~azure.ai.metricsadvisor.models.SQLConnectionStringCredentialEntity,
~azure.ai.metricsadvisor.models.DataLakeGen2SharedKeyCredentialEntity,
~azure.ai.metricsadvisor.models.ServicePrincipalCredentialEntity,
~azure.ai.metricsadvisor.models.ServicePrincipalInKVCredentialEntity]
:raises ~azure.core.exceptions.HttpResponseError:
.. admonition:: Example:
.. literalinclude:: ../samples/sample_credential_entities.py
:start-after: [START create_credential_entity]
:end-before: [END create_credential_entity]
:language: python
:dedent: 4
:caption: Create a credential entity
"""

credential_entity_request = None
if credential_entity.credential_entity_type in ["AzureSQLConnectionString",
"DataLakeGen2SharedKey", "ServicePrincipal", "ServicePrincipalInKV"]:
credential_entity_request = credential_entity._to_generated()

response_headers = self._client.create_credential( # type: ignore
credential_entity_request, # type: ignore
cls=lambda pipeline_response, _, response_headers: response_headers,
**kwargs
)
credential_entity_id = response_headers["Location"].split("credentials/")[1]
return self.get_credential_entity(credential_entity_id)

@distributed_trace
def list_credential_entities(
self,
**kwargs # type: Any
):
# type: (...) -> ItemPaged[CredentialEntityUnion]
"""List all credential entities.
:param skip: for paging, skipped number.
:type skip: int
:return: Pageable containing credential entities
:rtype: ~azure.core.paging.ItemPaged[Union[~azure.ai.metricsadvisor.models.SQLConnectionStringCredentialEntity,
~azure.ai.metricsadvisor.models.DataLakeGen2SharedKeyCredentialEntity,
~azure.ai.metricsadvisor.models.ServicePrincipalCredentialEntity,
~azure.ai.metricsadvisor.models.ServicePrincipalInKVCredentialEntity]]
:raises ~azure.core.exceptions.HttpResponseError:
.. admonition:: Example:
.. literalinclude:: ../samples/sample_credential_entities.py
:start-after: [START list_credential_entities]
:end-before: [END list_credential_entities]
:language: python
:dedent: 4
:caption: List all of the credential entities under the account
"""
return self._client.list_credentials( # type: ignore
cls=kwargs.pop(
"cls",
lambda credentials: [convert_to_credential_entity(credential) for credential in credentials]),
**kwargs
)

@distributed_trace
def update_credential_entity(
self,
credential_entity, # type: CredentialEntityUnion
**kwargs # type: Any
):
# type: (...) -> CredentialEntityUnion
"""Update a credential entity.
:param credential_entity: The new credential entity object
:type credential_entity: Union[~azure.ai.metricsadvisor.models.SQLConnectionStringCredentialEntity,
~azure.ai.metricsadvisor.models.DataLakeGen2SharedKeyCredentialEntity,
~azure.ai.metricsadvisor.models.ServicePrincipalCredentialEntity,
~azure.ai.metricsadvisor.models.ServicePrincipalInKVCredentialEntity]
:rtype: Union[~azure.ai.metricsadvisor.models.SQLConnectionStringCredentialEntity,
~azure.ai.metricsadvisor.models.DataLakeGen2SharedKeyCredentialEntity,
~azure.ai.metricsadvisor.models.ServicePrincipalCredentialEntity,
~azure.ai.metricsadvisor.models.ServicePrincipalInKVCredentialEntity]
:raises ~azure.core.exceptions.HttpResponseError:
.. admonition:: Example:
.. literalinclude:: ../samples/sample_credential_entities.py
:start-after: [START update_credential_entity]
:end-before: [END update_credential_entity]
:language: python
:dedent: 4
:caption: Update an existing credential entity
"""

if credential_entity.credential_entity_type in ["AzureSQLConnectionString",
"DataLakeGen2SharedKey", "ServicePrincipal", "ServicePrincipalInKV"]:
credential_entity_request = credential_entity._to_generated_patch()

updated_credential_entity = self._client.update_credential(
credential_entity.id,
credential_entity_request,
**kwargs
)

return convert_to_credential_entity(updated_credential_entity)

@distributed_trace
def delete_credential_entity(self, credential_entity_id, **kwargs):
# type: (str, Any) -> None
"""Delete a credential entity by its ID.
::param credential_entity_id: Credential entity unique ID.
:type credential_entity_id: str
:return: None
:rtype: None
:raises ~azure.core.exceptions.HttpResponseError:
.. admonition:: Example:
.. literalinclude:: ../samples/sample_credential_entities.py
:start-after: [START delete_credential_entity]
:end-before: [END delete_credential_entity]
:language: python
:dedent: 4
:caption: Delete a credential entity by its ID
"""

self._client.delete_credential(credential_id=credential_entity_id, **kwargs)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
# Licensed under the MIT License.
# ------------------------------------

VERSION = "1.0.0b4"
VERSION = "1.0.0"

SDK_MONIKER = "ai-metricsadvisor/{}".format(VERSION) # type: str
Loading