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

Add support for creating Clients from a previously read settings file #181

Merged
merged 3 commits into from
Aug 3, 2015

Conversation

catsby
Copy link
Contributor

@catsby catsby commented Jul 31, 2015

This adds support for initializing a client from the contents of a settings file that's stored in memory. At present, clients are created by supplying a file path to a settings file, but for my use case I have the contents of the file stored elsewhere, either encrypted in a database or something like HashiCorp's Vault

I originally tried modifying ClientFromPublishSettingsFileWithConfig to check the filePath parameter supplied, to see if it's valid XML and not a file path, but settled on duplicating and modifying the ClientFromPublishSettingsFile and ClientFromPublishSettingsFileWithConfig methods into ClientFromPublishSettingsData and ClientFromPublishSettingsDataWithConfig, instead. There is duplication, but I felt it was simpler to reason about.

Example usage, borrowing from the snippet in the README:

package main

import (
    "encoding/base64"
    "fmt"
    "io/ioutil"
    "log"

    "github.com/Azure/azure-sdk-for-go/management"
    "github.com/Azure/azure-sdk-for-go/management/hostedservice"
    "github.com/Azure/azure-sdk-for-go/management/virtualmachine"
    "github.com/Azure/azure-sdk-for-go/management/vmutils"
)

func main() {
    dnsName := "cts-settings-data-ex"
    storageAccount := "terraformtesting"
    location := "West US"
    vmSize := "Small"
    vmImage := "b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-14_04-LTS-amd64-server-20140724-en-us-30GB"
    userName := "testuser"
    userPassword := "Test123"

    publishSettingsContent, err := ioutil.ReadFile("downloaded.publishsettings")
    if err != nil {
        panic(err)
    }

    client, err := management.ClientFromPublishSettingsData(publishSettingsContent, "")
    if err != nil {
        panic(err)
    }

    log.Println("Client created...")

    // create hosted service
    if err := hostedservice.NewClient(client).CreateHostedService(hostedservice.CreateHostedServiceParameters{
        ServiceName: dnsName,
        Location:    location,
        Label:       base64.StdEncoding.EncodeToString([]byte(dnsName))}); err != nil {
        panic(err)
    }

    log.Println("Service created...")

    // create virtual machine
    role := vmutils.NewVMConfiguration(dnsName, vmSize)
    vmutils.ConfigureDeploymentFromPlatformImage(
        &role,
        vmImage,
        fmt.Sprintf("http://%s.blob.core.windows.net/sdktest/%s.vhd", storageAccount, dnsName),
        "")
    vmutils.ConfigureForLinux(&role, dnsName, userName, userPassword)
    vmutils.ConfigureWithPublicSSH(&role)

    log.Println("Creating ...")
    operationID, err := virtualmachine.NewClient(client).
        CreateDeployment(role, dnsName, virtualmachine.CreateDeploymentOptions{})
    if err != nil {
        panic(err)
    }
    if err := client.WaitForOperation(operationID, nil); err != nil {
        panic(err)
    }
    log.Println("Done!")
}

Suggestions on naming and testing are welcome.
Thanks!

@msftclas
Copy link

Hi @catsby, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!

In order for us to evaluate and accept your PR, we ask that you sign a contribution license agreement. It's all electronic and will take just minutes. I promise there's no faxing. https://cla.microsoft.com.

TTYL, MSBOT;

@ahmetb
Copy link
Contributor

ahmetb commented Jul 31, 2015

@catsby thanks for the pull request, looks like we need the CLA to be signed.

My only comment is you can totally call ClientFromPublishSettingsData from ClientFromPublishSettingsFile by first reading the file with ioutil.ReadFile and then passing the contents.

Having duplicated logic is something we usually really want to avoid as people usually forget maintaining both at the same time.

@msftclas
Copy link

@catsby, Thanks for signing the contribution license agreement so quickly! Actual humans will now validate the agreement and then evaluate the PR.

Thanks, MSBOT;

@catsby
Copy link
Contributor Author

catsby commented Jul 31, 2015

Hey @ahmetalpbalkan thanks for the reply

Having duplicated logic is something we usually really want to avoid as people usually forget maintaining both at the same time.

I've refactored some to consolidate and remove duplication, as well as preserve backwards compatibility. I think... can you take a look? Thanks!

@ahmetb
Copy link
Contributor

ahmetb commented Jul 31, 2015

@catsby I think we can even remove duplication in xml.Unmarshal. For now, by creating another method we have two paths that go like this:

func A()+---------+   
                  |   
                  v   
                func c()
                  ^   
                  |   
func B()+---------+   

However if you could call ClientFromPublishSettingsData from ClientFromPublishSettingsFile without creating another method (as in func c above), we'll have:

  +--------------------+           
  |      caller        |           
  +--+------------+----+           
     |            |                
     |            |                
     |            |                
     |            |                
     |            |                
     v            v                
func A()+--->func B()+------>func c()

which really enforces a single path. Both are equally OK, however I find latter easier to read. :-) If you could also fix that, the rest is LGTM.

@catsby
Copy link
Contributor Author

catsby commented Aug 3, 2015

Hey @ahmetalpbalkan , thanks for the continued help here. I hate to be difficult but I'm not sure I follow what you're suggesting, though I did just push 902e8bb to remove some duplication of xml.Unmarshall.

However if you could call ClientFromPublishSettingsData from ClientFromPublishSettingsFile without creating another method (as in func c above), we'll have

By this, do you mean for ClientFromPublishSettingsFile to determine if the filePath input is actually a file path, or contents of a file, and then choose ClientFromPublishSettingsFileWithConfig or ClientFromPublishSettingsData accordingly? I avoided this mostly because I didn't want to change the signature/purpose of any existing methods with this addition, specifically any exported methods.

With 902e8bb , both ClientFromPublishSettingsFile and ClientFromPublishSettingsData pass on a []byte slice to clientFromPublishData, which does the unmarshalling.

@ahmetb
Copy link
Contributor

ahmetb commented Aug 3, 2015

LGTM. Thanks!

ahmetb added a commit that referenced this pull request Aug 3, 2015
Add support for creating Clients from a previously read settings file
@ahmetb ahmetb merged commit 0a2626d into Azure:master Aug 3, 2015
@catsby
Copy link
Contributor Author

catsby commented Aug 3, 2015

Thanks so much @ahmetalpbalkan !

richardpark-msft pushed a commit to richardpark-msft/azure-sdk-for-go that referenced this pull request Aug 5, 2021
benbp pushed a commit to benbp/azure-sdk-for-go that referenced this pull request Sep 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants