2015-12-08 23:30:12 +01:00
|
|
|
package azurerm
|
|
|
|
|
|
|
|
import (
|
2017-03-08 22:57:51 +01:00
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
2016-01-05 22:43:52 +01:00
|
|
|
"fmt"
|
2017-01-02 18:51:26 +01:00
|
|
|
"log"
|
2016-02-18 22:33:01 +01:00
|
|
|
"reflect"
|
2015-12-08 23:30:12 +01:00
|
|
|
"strings"
|
2016-06-11 00:37:14 +02:00
|
|
|
"sync"
|
|
|
|
|
2017-01-02 13:09:26 +01:00
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/resources/resources"
|
2016-01-29 16:48:20 +01:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2016-01-08 16:55:46 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/mutexkv"
|
2016-02-18 22:33:01 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2015-12-08 23:30:12 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2016-02-18 22:33:01 +01:00
|
|
|
riviera "github.com/jen20/riviera/azure"
|
2015-12-08 23:30:12 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Provider returns a terraform.ResourceProvider.
|
|
|
|
func Provider() terraform.ResourceProvider {
|
2016-10-24 03:26:06 +02:00
|
|
|
var p *schema.Provider
|
|
|
|
p = &schema.Provider{
|
2015-12-08 23:30:12 +01:00
|
|
|
Schema: map[string]*schema.Schema{
|
2016-06-01 22:17:21 +02:00
|
|
|
"subscription_id": {
|
2015-12-08 23:30:12 +01:00
|
|
|
Type: schema.TypeString,
|
2015-12-09 02:25:05 +01:00
|
|
|
Required: true,
|
2015-12-09 00:50:48 +01:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("ARM_SUBSCRIPTION_ID", ""),
|
2015-12-08 23:30:12 +01:00
|
|
|
},
|
|
|
|
|
2016-06-01 22:17:21 +02:00
|
|
|
"client_id": {
|
2015-12-08 23:30:12 +01:00
|
|
|
Type: schema.TypeString,
|
2015-12-09 02:25:05 +01:00
|
|
|
Required: true,
|
2015-12-08 23:30:12 +01:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("ARM_CLIENT_ID", ""),
|
|
|
|
},
|
|
|
|
|
2016-06-01 22:17:21 +02:00
|
|
|
"client_secret": {
|
2015-12-08 23:30:12 +01:00
|
|
|
Type: schema.TypeString,
|
2015-12-09 02:25:05 +01:00
|
|
|
Required: true,
|
2015-12-08 23:30:12 +01:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("ARM_CLIENT_SECRET", ""),
|
|
|
|
},
|
|
|
|
|
2016-06-01 22:17:21 +02:00
|
|
|
"tenant_id": {
|
2015-12-08 23:30:12 +01:00
|
|
|
Type: schema.TypeString,
|
2015-12-09 02:25:05 +01:00
|
|
|
Required: true,
|
2015-12-08 23:30:12 +01:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("ARM_TENANT_ID", ""),
|
|
|
|
},
|
2017-01-02 13:09:26 +01:00
|
|
|
|
2017-01-04 13:00:14 +01:00
|
|
|
"environment": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("ARM_ENVIRONMENT", "public"),
|
|
|
|
},
|
|
|
|
|
2017-01-02 13:09:26 +01:00
|
|
|
"skip_provider_registration": {
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("ARM_SKIP_PROVIDER_REGISTRATION", false),
|
|
|
|
},
|
2015-12-08 23:30:12 +01:00
|
|
|
},
|
|
|
|
|
2016-10-20 12:29:26 +02:00
|
|
|
DataSourcesMap: map[string]*schema.Resource{
|
|
|
|
"azurerm_client_config": dataSourceArmClientConfig(),
|
|
|
|
},
|
|
|
|
|
2015-12-08 23:30:12 +01:00
|
|
|
ResourcesMap: map[string]*schema.Resource{
|
2016-06-01 22:17:21 +02:00
|
|
|
// These resources use the Azure ARM SDK
|
2017-01-02 15:37:45 +01:00
|
|
|
"azurerm_availability_set": resourceArmAvailabilitySet(),
|
|
|
|
"azurerm_cdn_endpoint": resourceArmCdnEndpoint(),
|
|
|
|
"azurerm_cdn_profile": resourceArmCdnProfile(),
|
|
|
|
"azurerm_container_registry": resourceArmContainerRegistry(),
|
2017-02-13 17:33:50 +01:00
|
|
|
"azurerm_container_service": resourceArmContainerService(),
|
2016-10-07 20:14:26 +02:00
|
|
|
|
2017-01-02 17:32:07 +01:00
|
|
|
"azurerm_eventhub": resourceArmEventHub(),
|
|
|
|
"azurerm_eventhub_authorization_rule": resourceArmEventHubAuthorizationRule(),
|
|
|
|
"azurerm_eventhub_consumer_group": resourceArmEventHubConsumerGroup(),
|
|
|
|
"azurerm_eventhub_namespace": resourceArmEventHubNamespace(),
|
2016-10-25 16:50:07 +02:00
|
|
|
|
2016-10-07 20:14:26 +02:00
|
|
|
"azurerm_lb": resourceArmLoadBalancer(),
|
|
|
|
"azurerm_lb_backend_address_pool": resourceArmLoadBalancerBackendAddressPool(),
|
|
|
|
"azurerm_lb_nat_rule": resourceArmLoadBalancerNatRule(),
|
|
|
|
"azurerm_lb_nat_pool": resourceArmLoadBalancerNatPool(),
|
|
|
|
"azurerm_lb_probe": resourceArmLoadBalancerProbe(),
|
|
|
|
"azurerm_lb_rule": resourceArmLoadBalancerRule(),
|
|
|
|
|
2017-03-03 23:46:33 +01:00
|
|
|
"azurerm_managed_disk": resourceArmManagedDisk(),
|
|
|
|
|
2016-10-20 12:30:30 +02:00
|
|
|
"azurerm_key_vault": resourceArmKeyVault(),
|
2016-06-11 00:37:14 +02:00
|
|
|
"azurerm_local_network_gateway": resourceArmLocalNetworkGateway(),
|
|
|
|
"azurerm_network_interface": resourceArmNetworkInterface(),
|
|
|
|
"azurerm_network_security_group": resourceArmNetworkSecurityGroup(),
|
|
|
|
"azurerm_network_security_rule": resourceArmNetworkSecurityRule(),
|
|
|
|
"azurerm_public_ip": resourceArmPublicIp(),
|
2016-12-18 15:23:29 +01:00
|
|
|
"azurerm_redis_cache": resourceArmRedisCache(),
|
2016-06-11 00:37:14 +02:00
|
|
|
"azurerm_route": resourceArmRoute(),
|
|
|
|
"azurerm_route_table": resourceArmRouteTable(),
|
2016-08-15 19:00:00 +02:00
|
|
|
"azurerm_servicebus_namespace": resourceArmServiceBusNamespace(),
|
2016-10-03 12:22:18 +02:00
|
|
|
"azurerm_servicebus_subscription": resourceArmServiceBusSubscription(),
|
2016-09-29 19:07:25 +02:00
|
|
|
"azurerm_servicebus_topic": resourceArmServiceBusTopic(),
|
2016-06-11 00:37:14 +02:00
|
|
|
"azurerm_storage_account": resourceArmStorageAccount(),
|
|
|
|
"azurerm_storage_blob": resourceArmStorageBlob(),
|
|
|
|
"azurerm_storage_container": resourceArmStorageContainer(),
|
2016-09-06 11:10:27 +02:00
|
|
|
"azurerm_storage_share": resourceArmStorageShare(),
|
2016-06-11 00:37:14 +02:00
|
|
|
"azurerm_storage_queue": resourceArmStorageQueue(),
|
2016-07-27 23:49:43 +02:00
|
|
|
"azurerm_storage_table": resourceArmStorageTable(),
|
2016-06-11 00:37:14 +02:00
|
|
|
"azurerm_subnet": resourceArmSubnet(),
|
|
|
|
"azurerm_template_deployment": resourceArmTemplateDeployment(),
|
2016-08-01 00:46:15 +02:00
|
|
|
"azurerm_traffic_manager_endpoint": resourceArmTrafficManagerEndpoint(),
|
|
|
|
"azurerm_traffic_manager_profile": resourceArmTrafficManagerProfile(),
|
2016-11-09 16:13:48 +01:00
|
|
|
"azurerm_virtual_machine_extension": resourceArmVirtualMachineExtensions(),
|
2016-06-11 00:37:14 +02:00
|
|
|
"azurerm_virtual_machine": resourceArmVirtualMachine(),
|
|
|
|
"azurerm_virtual_machine_scale_set": resourceArmVirtualMachineScaleSet(),
|
|
|
|
"azurerm_virtual_network": resourceArmVirtualNetwork(),
|
2016-08-13 20:37:46 +02:00
|
|
|
"azurerm_virtual_network_peering": resourceArmVirtualNetworkPeering(),
|
2016-06-01 22:17:21 +02:00
|
|
|
|
|
|
|
// These resources use the Riviera SDK
|
|
|
|
"azurerm_dns_a_record": resourceArmDnsARecord(),
|
|
|
|
"azurerm_dns_aaaa_record": resourceArmDnsAAAARecord(),
|
|
|
|
"azurerm_dns_cname_record": resourceArmDnsCNameRecord(),
|
|
|
|
"azurerm_dns_mx_record": resourceArmDnsMxRecord(),
|
|
|
|
"azurerm_dns_ns_record": resourceArmDnsNsRecord(),
|
|
|
|
"azurerm_dns_srv_record": resourceArmDnsSrvRecord(),
|
|
|
|
"azurerm_dns_txt_record": resourceArmDnsTxtRecord(),
|
|
|
|
"azurerm_dns_zone": resourceArmDnsZone(),
|
|
|
|
"azurerm_resource_group": resourceArmResourceGroup(),
|
|
|
|
"azurerm_search_service": resourceArmSearchService(),
|
|
|
|
"azurerm_sql_database": resourceArmSqlDatabase(),
|
|
|
|
"azurerm_sql_firewall_rule": resourceArmSqlFirewallRule(),
|
|
|
|
"azurerm_sql_server": resourceArmSqlServer(),
|
2015-12-08 23:30:12 +01:00
|
|
|
},
|
|
|
|
}
|
2016-10-24 03:26:06 +02:00
|
|
|
|
2016-11-09 16:49:14 +01:00
|
|
|
p.ConfigureFunc = providerConfigure(p)
|
|
|
|
|
2016-10-24 03:26:06 +02:00
|
|
|
return p
|
2015-12-08 23:30:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config is the configuration structure used to instantiate a
|
|
|
|
// new Azure management client.
|
|
|
|
type Config struct {
|
|
|
|
ManagementURL string
|
|
|
|
|
2017-01-02 13:09:26 +01:00
|
|
|
SubscriptionID string
|
|
|
|
ClientID string
|
|
|
|
ClientSecret string
|
|
|
|
TenantID string
|
2017-01-04 13:00:14 +01:00
|
|
|
Environment string
|
2017-01-02 13:09:26 +01:00
|
|
|
SkipProviderRegistration bool
|
2016-04-22 01:50:47 +02:00
|
|
|
|
|
|
|
validateCredentialsOnce sync.Once
|
2015-12-08 23:30:12 +01:00
|
|
|
}
|
|
|
|
|
2016-04-22 01:50:47 +02:00
|
|
|
func (c *Config) validate() error {
|
2016-01-29 16:48:20 +01:00
|
|
|
var err *multierror.Error
|
|
|
|
|
|
|
|
if c.SubscriptionID == "" {
|
|
|
|
err = multierror.Append(err, fmt.Errorf("Subscription ID must be configured for the AzureRM provider"))
|
|
|
|
}
|
|
|
|
if c.ClientID == "" {
|
|
|
|
err = multierror.Append(err, fmt.Errorf("Client ID must be configured for the AzureRM provider"))
|
|
|
|
}
|
|
|
|
if c.ClientSecret == "" {
|
|
|
|
err = multierror.Append(err, fmt.Errorf("Client Secret must be configured for the AzureRM provider"))
|
|
|
|
}
|
|
|
|
if c.TenantID == "" {
|
|
|
|
err = multierror.Append(err, fmt.Errorf("Tenant ID must be configured for the AzureRM provider"))
|
|
|
|
}
|
2017-01-04 13:00:14 +01:00
|
|
|
if c.Environment == "" {
|
|
|
|
err = multierror.Append(err, fmt.Errorf("Environment must be configured for the AzureRM provider"))
|
|
|
|
}
|
2016-01-29 16:48:20 +01:00
|
|
|
|
|
|
|
return err.ErrorOrNil()
|
|
|
|
}
|
|
|
|
|
2016-10-24 03:26:06 +02:00
|
|
|
func providerConfigure(p *schema.Provider) schema.ConfigureFunc {
|
|
|
|
return func(d *schema.ResourceData) (interface{}, error) {
|
|
|
|
config := &Config{
|
2017-01-02 13:09:26 +01:00
|
|
|
SubscriptionID: d.Get("subscription_id").(string),
|
|
|
|
ClientID: d.Get("client_id").(string),
|
|
|
|
ClientSecret: d.Get("client_secret").(string),
|
|
|
|
TenantID: d.Get("tenant_id").(string),
|
2017-01-04 13:00:14 +01:00
|
|
|
Environment: d.Get("environment").(string),
|
2017-01-02 13:09:26 +01:00
|
|
|
SkipProviderRegistration: d.Get("skip_provider_registration").(bool),
|
2016-10-24 03:26:06 +02:00
|
|
|
}
|
2015-12-08 23:30:12 +01:00
|
|
|
|
2016-10-24 03:26:06 +02:00
|
|
|
if err := config.validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-05 22:43:52 +01:00
|
|
|
|
2016-10-24 03:26:06 +02:00
|
|
|
client, err := config.getArmClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-10-24 03:26:06 +02:00
|
|
|
|
2016-10-25 20:29:24 +02:00
|
|
|
client.StopContext = p.StopContext()
|
2016-10-25 21:00:36 +02:00
|
|
|
|
2017-03-07 17:34:07 +01:00
|
|
|
// replaces the context between tests
|
|
|
|
p.MetaReset = func() error {
|
|
|
|
client.StopContext = p.StopContext()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-02 13:09:26 +01:00
|
|
|
// List all the available providers and their registration state to avoid unnecessary
|
|
|
|
// requests. This also lets us check if the provider credentials are correct.
|
|
|
|
providerList, err := client.providers.List(nil, "")
|
2016-10-24 03:26:06 +02:00
|
|
|
if err != nil {
|
2017-01-02 13:09:26 +01:00
|
|
|
return nil, fmt.Errorf("Unable to list provider registration status, it is possible that this is due to invalid "+
|
|
|
|
"credentials or the service principal does not have permission to use the Resource Manager API, Azure "+
|
|
|
|
"error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !config.SkipProviderRegistration {
|
|
|
|
err = registerAzureResourceProvidersWithSubscription(*providerList.Value, client.providers)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-10-24 03:26:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|
2015-12-08 23:30:12 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 13:09:26 +01:00
|
|
|
func registerProviderWithSubscription(providerName string, client resources.ProvidersClient) error {
|
|
|
|
_, err := client.Register(providerName)
|
2016-04-22 01:50:47 +02:00
|
|
|
if err != nil {
|
2017-01-02 13:09:26 +01:00
|
|
|
return fmt.Errorf("Cannot register provider %s with Azure Resource Manager: %s.", providerName, err)
|
2016-04-22 01:50:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var providerRegistrationOnce sync.Once
|
|
|
|
|
2016-01-05 22:43:52 +01:00
|
|
|
// registerAzureResourceProvidersWithSubscription uses the providers client to register
|
|
|
|
// all Azure resource providers which the Terraform provider may require (regardless of
|
|
|
|
// whether they are actually used by the configuration or not). It was confirmed by Microsoft
|
|
|
|
// that this is the approach their own internal tools also take.
|
2017-01-02 13:09:26 +01:00
|
|
|
func registerAzureResourceProvidersWithSubscription(providerList []resources.Provider, client resources.ProvidersClient) error {
|
2016-04-22 01:50:47 +02:00
|
|
|
var err error
|
|
|
|
providerRegistrationOnce.Do(func() {
|
2017-01-02 13:09:26 +01:00
|
|
|
providers := map[string]struct{}{
|
2017-01-02 18:51:26 +01:00
|
|
|
"Microsoft.Compute": struct{}{},
|
|
|
|
"Microsoft.Cache": struct{}{},
|
|
|
|
"Microsoft.ContainerRegistry": struct{}{},
|
2017-02-13 17:33:50 +01:00
|
|
|
"Microsoft.ContainerService": struct{}{},
|
2017-01-02 18:51:26 +01:00
|
|
|
"Microsoft.Network": struct{}{},
|
|
|
|
"Microsoft.Cdn": struct{}{},
|
|
|
|
"Microsoft.Storage": struct{}{},
|
|
|
|
"Microsoft.Sql": struct{}{},
|
|
|
|
"Microsoft.Search": struct{}{},
|
|
|
|
"Microsoft.Resources": struct{}{},
|
|
|
|
"Microsoft.ServiceBus": struct{}{},
|
|
|
|
"Microsoft.KeyVault": struct{}{},
|
|
|
|
"Microsoft.EventHub": struct{}{},
|
2017-01-02 13:09:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// filter out any providers already registered
|
|
|
|
for _, p := range providerList {
|
|
|
|
if _, ok := providers[*p.Namespace]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.ToLower(*p.RegistrationState) == "registered" {
|
|
|
|
log.Printf("[DEBUG] Skipping provider registration for namespace %s\n", *p.Namespace)
|
|
|
|
delete(providers, *p.Namespace)
|
|
|
|
}
|
2016-12-18 15:23:29 +01:00
|
|
|
}
|
2016-04-22 01:50:47 +02:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(len(providers))
|
2017-01-02 13:09:26 +01:00
|
|
|
for providerName := range providers {
|
2016-04-22 01:50:47 +02:00
|
|
|
go func(p string) {
|
|
|
|
defer wg.Done()
|
2017-01-02 13:09:26 +01:00
|
|
|
log.Printf("[DEBUG] Registering provider with namespace %s\n", p)
|
2016-04-22 01:50:47 +02:00
|
|
|
if innerErr := registerProviderWithSubscription(p, client); err != nil {
|
|
|
|
err = innerErr
|
|
|
|
}
|
|
|
|
}(providerName)
|
2016-01-05 22:43:52 +01:00
|
|
|
}
|
2016-04-22 01:50:47 +02:00
|
|
|
wg.Wait()
|
|
|
|
})
|
2016-01-05 22:43:52 +01:00
|
|
|
|
2016-04-22 01:50:47 +02:00
|
|
|
return err
|
2016-01-05 22:43:52 +01:00
|
|
|
}
|
|
|
|
|
2016-01-08 16:55:46 +01:00
|
|
|
// armMutexKV is the instance of MutexKV for ARM resources
|
|
|
|
var armMutexKV = mutexkv.NewMutexKV()
|
2016-02-18 22:33:01 +01:00
|
|
|
|
|
|
|
func azureStateRefreshFunc(resourceURI string, client *ArmClient, command riviera.APICall) resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
req := client.rivieraClient.NewRequestForURI(resourceURI)
|
|
|
|
req.Command = command
|
|
|
|
|
|
|
|
res, err := req.Execute()
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", fmt.Errorf("Error executing %T command in azureStateRefreshFunc", req.Command)
|
|
|
|
}
|
|
|
|
|
|
|
|
var value reflect.Value
|
|
|
|
if reflect.ValueOf(res.Parsed).Kind() == reflect.Ptr {
|
|
|
|
value = reflect.ValueOf(res.Parsed).Elem()
|
|
|
|
} else {
|
|
|
|
value = reflect.ValueOf(res.Parsed)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < value.NumField(); i++ { // iterates through every struct type field
|
|
|
|
tag := value.Type().Field(i).Tag // returns the tag string
|
|
|
|
tagValue := tag.Get("mapstructure")
|
|
|
|
if tagValue == "provisioningState" {
|
|
|
|
return res.Parsed, value.Field(i).Elem().String(), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
panic(fmt.Errorf("azureStateRefreshFunc called on structure %T with no mapstructure:provisioningState tag. This is a bug", res.Parsed))
|
|
|
|
}
|
|
|
|
}
|
2016-09-27 18:15:02 +02:00
|
|
|
|
|
|
|
// Resource group names can be capitalised, but we store them in lowercase.
|
|
|
|
// Use a custom diff function to avoid creation of new resources.
|
|
|
|
func resourceAzurermResourceGroupNameDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
|
|
|
|
return strings.ToLower(old) == strings.ToLower(new)
|
|
|
|
}
|
2017-02-22 21:30:07 +01:00
|
|
|
|
|
|
|
// ignoreCaseDiffSuppressFunc is a DiffSuppressFunc from helper/schema that is
|
|
|
|
// used to ignore any case-changes in a return value.
|
|
|
|
func ignoreCaseDiffSuppressFunc(k, old, new string, d *schema.ResourceData) bool {
|
|
|
|
return strings.ToLower(old) == strings.ToLower(new)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignoreCaseStateFunc is a StateFunc from helper/schema that converts the
|
|
|
|
// supplied value to lower before saving to state for consistency.
|
|
|
|
func ignoreCaseStateFunc(val interface{}) string {
|
|
|
|
return strings.ToLower(val.(string))
|
|
|
|
}
|
2017-03-08 22:57:51 +01:00
|
|
|
|
|
|
|
func userDataStateFunc(v interface{}) string {
|
|
|
|
switch s := v.(type) {
|
|
|
|
case string:
|
|
|
|
s = base64Encode(s)
|
|
|
|
hash := sha1.Sum([]byte(s))
|
|
|
|
return hex.EncodeToString(hash[:])
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 00:37:28 +02:00
|
|
|
// base64Encode encodes data if the input isn't already encoded using
|
2017-03-08 22:57:51 +01:00
|
|
|
// base64.StdEncoding.EncodeToString. If the input is already base64 encoded,
|
|
|
|
// return the original input unchanged.
|
|
|
|
func base64Encode(data string) string {
|
|
|
|
// Check whether the data is already Base64 encoded; don't double-encode
|
|
|
|
if isBase64Encoded(data) {
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
// data has not been encoded encode and return
|
|
|
|
return base64.StdEncoding.EncodeToString([]byte(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
func isBase64Encoded(data string) bool {
|
|
|
|
_, err := base64.StdEncoding.DecodeString(data)
|
|
|
|
return err == nil
|
|
|
|
}
|