All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.azure.resourcemanager.datafactory.models.ServicePrincipalCredential Maven / Gradle / Ivy

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.resourcemanager.datafactory.models;

import com.azure.core.annotation.Fluent;
import com.azure.core.util.logging.ClientLogger;
import com.azure.json.JsonReader;
import com.azure.json.JsonToken;
import com.azure.json.JsonWriter;
import com.azure.resourcemanager.datafactory.fluent.models.ServicePrincipalCredentialTypeProperties;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Service principal credential.
 */
@Fluent
public final class ServicePrincipalCredential extends Credential {
    /*
     * Type of credential.
     */
    private String type = "ServicePrincipal";

    /*
     * Service Principal credential properties.
     */
    private ServicePrincipalCredentialTypeProperties innerTypeProperties
        = new ServicePrincipalCredentialTypeProperties();

    /**
     * Creates an instance of ServicePrincipalCredential class.
     */
    public ServicePrincipalCredential() {
    }

    /**
     * Get the type property: Type of credential.
     * 
     * @return the type value.
     */
    @Override
    public String type() {
        return this.type;
    }

    /**
     * Get the innerTypeProperties property: Service Principal credential properties.
     * 
     * @return the innerTypeProperties value.
     */
    private ServicePrincipalCredentialTypeProperties innerTypeProperties() {
        return this.innerTypeProperties;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ServicePrincipalCredential withDescription(String description) {
        super.withDescription(description);
        return this;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ServicePrincipalCredential withAnnotations(List annotations) {
        super.withAnnotations(annotations);
        return this;
    }

    /**
     * Get the servicePrincipalId property: The app ID of the service principal used to authenticate.
     * 
     * @return the servicePrincipalId value.
     */
    public Object servicePrincipalId() {
        return this.innerTypeProperties() == null ? null : this.innerTypeProperties().servicePrincipalId();
    }

    /**
     * Set the servicePrincipalId property: The app ID of the service principal used to authenticate.
     * 
     * @param servicePrincipalId the servicePrincipalId value to set.
     * @return the ServicePrincipalCredential object itself.
     */
    public ServicePrincipalCredential withServicePrincipalId(Object servicePrincipalId) {
        if (this.innerTypeProperties() == null) {
            this.innerTypeProperties = new ServicePrincipalCredentialTypeProperties();
        }
        this.innerTypeProperties().withServicePrincipalId(servicePrincipalId);
        return this;
    }

    /**
     * Get the servicePrincipalKey property: The key of the service principal used to authenticate.
     * 
     * @return the servicePrincipalKey value.
     */
    public AzureKeyVaultSecretReference servicePrincipalKey() {
        return this.innerTypeProperties() == null ? null : this.innerTypeProperties().servicePrincipalKey();
    }

    /**
     * Set the servicePrincipalKey property: The key of the service principal used to authenticate.
     * 
     * @param servicePrincipalKey the servicePrincipalKey value to set.
     * @return the ServicePrincipalCredential object itself.
     */
    public ServicePrincipalCredential withServicePrincipalKey(AzureKeyVaultSecretReference servicePrincipalKey) {
        if (this.innerTypeProperties() == null) {
            this.innerTypeProperties = new ServicePrincipalCredentialTypeProperties();
        }
        this.innerTypeProperties().withServicePrincipalKey(servicePrincipalKey);
        return this;
    }

    /**
     * Get the tenant property: The ID of the tenant to which the service principal belongs.
     * 
     * @return the tenant value.
     */
    public Object tenant() {
        return this.innerTypeProperties() == null ? null : this.innerTypeProperties().tenant();
    }

    /**
     * Set the tenant property: The ID of the tenant to which the service principal belongs.
     * 
     * @param tenant the tenant value to set.
     * @return the ServicePrincipalCredential object itself.
     */
    public ServicePrincipalCredential withTenant(Object tenant) {
        if (this.innerTypeProperties() == null) {
            this.innerTypeProperties = new ServicePrincipalCredentialTypeProperties();
        }
        this.innerTypeProperties().withTenant(tenant);
        return this;
    }

    /**
     * Validates the instance.
     * 
     * @throws IllegalArgumentException thrown if the instance is not valid.
     */
    @Override
    public void validate() {
        if (innerTypeProperties() == null) {
            throw LOGGER.atError()
                .log(new IllegalArgumentException(
                    "Missing required property innerTypeProperties in model ServicePrincipalCredential"));
        } else {
            innerTypeProperties().validate();
        }
    }

    private static final ClientLogger LOGGER = new ClientLogger(ServicePrincipalCredential.class);

    /**
     * {@inheritDoc}
     */
    @Override
    public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
        jsonWriter.writeStartObject();
        jsonWriter.writeStringField("description", description());
        jsonWriter.writeArrayField("annotations", annotations(), (writer, element) -> writer.writeUntyped(element));
        jsonWriter.writeJsonField("typeProperties", this.innerTypeProperties);
        jsonWriter.writeStringField("type", this.type);
        if (additionalProperties() != null) {
            for (Map.Entry additionalProperty : additionalProperties().entrySet()) {
                jsonWriter.writeUntypedField(additionalProperty.getKey(), additionalProperty.getValue());
            }
        }
        return jsonWriter.writeEndObject();
    }

    /**
     * Reads an instance of ServicePrincipalCredential from the JsonReader.
     * 
     * @param jsonReader The JsonReader being read.
     * @return An instance of ServicePrincipalCredential if the JsonReader was pointing to an instance of it, or null if
     * it was pointing to JSON null.
     * @throws IllegalStateException If the deserialized JSON object was missing any required properties.
     * @throws IOException If an error occurs while reading the ServicePrincipalCredential.
     */
    public static ServicePrincipalCredential fromJson(JsonReader jsonReader) throws IOException {
        return jsonReader.readObject(reader -> {
            ServicePrincipalCredential deserializedServicePrincipalCredential = new ServicePrincipalCredential();
            Map additionalProperties = null;
            while (reader.nextToken() != JsonToken.END_OBJECT) {
                String fieldName = reader.getFieldName();
                reader.nextToken();

                if ("description".equals(fieldName)) {
                    deserializedServicePrincipalCredential.withDescription(reader.getString());
                } else if ("annotations".equals(fieldName)) {
                    List annotations = reader.readArray(reader1 -> reader1.readUntyped());
                    deserializedServicePrincipalCredential.withAnnotations(annotations);
                } else if ("typeProperties".equals(fieldName)) {
                    deserializedServicePrincipalCredential.innerTypeProperties
                        = ServicePrincipalCredentialTypeProperties.fromJson(reader);
                } else if ("type".equals(fieldName)) {
                    deserializedServicePrincipalCredential.type = reader.getString();
                } else {
                    if (additionalProperties == null) {
                        additionalProperties = new LinkedHashMap<>();
                    }

                    additionalProperties.put(fieldName, reader.readUntyped());
                }
            }
            deserializedServicePrincipalCredential.withAdditionalProperties(additionalProperties);

            return deserializedServicePrincipalCredential;
        });
    }
}