Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3767 from patrickdillon/azure-session
pkg/asset/installconfig/azure: create reusable session
- Loading branch information
Showing
16 changed files
with
135 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package azure | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/pkg/errors" | ||
|
||
typesazure "github.com/openshift/installer/pkg/types/azure" | ||
) | ||
|
||
// Metadata holds additional metadata for InstallConfig resources that | ||
// does not need to be user-supplied (e.g. because it can be retrieved | ||
// from external APIs). | ||
type Metadata struct { | ||
session *Session | ||
client *Client | ||
dnsCfg *DNSConfig | ||
|
||
// CloudName indicates the Azure cloud environment (e.g. public, gov't). | ||
CloudName typesazure.CloudEnvironment `json:"cloudName,omitempty"` | ||
|
||
mutex sync.Mutex | ||
} | ||
|
||
// NewMetadata initializes a new Metadata object. | ||
func NewMetadata(cloudName typesazure.CloudEnvironment) *Metadata { | ||
return &Metadata{CloudName: cloudName} | ||
} | ||
|
||
// Session holds an Azure session which can be used for Azure API calls | ||
// during asset generation. | ||
func (m *Metadata) Session() (*Session, error) { | ||
m.mutex.Lock() | ||
defer m.mutex.Unlock() | ||
|
||
return m.unlockedSession() | ||
} | ||
|
||
func (m *Metadata) unlockedSession() (*Session, error) { | ||
if m.session == nil { | ||
var err error | ||
m.session, err = GetSession(m.CloudName) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "creating Azure session") | ||
} | ||
} | ||
|
||
return m.session, nil | ||
} | ||
|
||
// Client holds an Azure Client that implements calls to the Azure API. | ||
func (m *Metadata) Client() (*Client, error) { | ||
if m.client == nil { | ||
ssn, err := m.Session() | ||
if err != nil { | ||
return nil, err | ||
} | ||
m.client = NewClient(ssn) | ||
} | ||
return m.client, nil | ||
} | ||
|
||
// DNSConfig holds an Azure DNSConfig Client that implements calls to the Azure API. | ||
func (m *Metadata) DNSConfig() (*DNSConfig, error) { | ||
if m.dnsCfg == nil { | ||
ssn, err := m.Session() | ||
if err != nil { | ||
return nil, err | ||
} | ||
m.dnsCfg = NewDNSConfig(ssn) | ||
} | ||
return m.dnsCfg, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.