Skip to content

Commit 78f910b

Browse files
committed
Switches to new library methods
1 parent 795a373 commit 78f910b

33 files changed

Lines changed: 218 additions & 276 deletions

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ toolchain go1.24.5
66

77
require (
88
github.com/Azure/azure-extension-foundation v0.0.0-20250620154556-caff9e3c3c5c
9-
github.com/Azure/azure-extension-platform v0.0.0-20250107200156-aa20f765d49f
9+
github.com/Azure/azure-extension-platform v0.0.0-20260107210613-2a62cc200c34
1010
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible
1111
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.16.0
1212
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/Azure/azure-extension-platform v0.0.0-20240610175536-404c704f82f8 h1:
44
github.com/Azure/azure-extension-platform v0.0.0-20240610175536-404c704f82f8/go.mod h1:nEQQIC3RKmMnpdc+RakYHIdu556jdcHv67ML8PdsQeQ=
55
github.com/Azure/azure-extension-platform v0.0.0-20250107200156-aa20f765d49f h1:ddsUz/suc9txCMz/xWOslqNMvzhbWFMTflUrbcMNoSw=
66
github.com/Azure/azure-extension-platform v0.0.0-20250107200156-aa20f765d49f/go.mod h1:0458BvQsi5ch6kn+KZtI5m88Z3L9UFXdoY1+6nKdivY=
7+
github.com/Azure/azure-extension-platform v0.0.0-20260107210613-2a62cc200c34 h1:7bEC4DJC4w0gx7SBy7M7Q2qi6ckmHcnnlFJzo+X/gi4=
8+
github.com/Azure/azure-extension-platform v0.0.0-20260107210613-2a62cc200c34/go.mod h1:0458BvQsi5ch6kn+KZtI5m88Z3L9UFXdoY1+6nKdivY=
79
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible h1:fcYLmCpyNYRnvJbPerq7U0hS+6+I79yEDJBqVNcqUzU=
810
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
911
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.16.0 h1:JZg6HRh6W6U4OLl6lk7BZ7BLisIzM9dG1R50zUk9C/M=

internal/cmds/cmds.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func enable(ctx *log.Context, h types.HandlerEnvironment, report *types.RunComma
208208
extensionEvents.LogErrorEvent("enable", errMessage)
209209
return "",
210210
"",
211-
handlersettings.InternalWrapErrorWithClarification(err, fmt.Sprintf("File downloads failed. Use either a public script URI that points to .sh file, Azure storage blob SAS URI or storage blob accessible by a managed identity and retry. If managed identity is used, make sure it has been given access to container of storage blob '%s' with 'Storage Blob Data Reader' role assignment. In case of user-assigned identity, make sure you add it under VM's identity. For more info, refer https://aka.ms/RunCommandManagedLinux", download.GetUriForLogging(cfg.ScriptURI()))),
211+
vmextension.CreateWrappedErrorWithClarification(err, fmt.Sprintf("File downloads failed. Use either a public script URI that points to .sh file, Azure storage blob SAS URI or storage blob accessible by a managed identity and retry. If managed identity is used, make sure it has been given access to container of storage blob '%s' with 'Storage Blob Data Reader' role assignment. In case of user-assigned identity, make sure you add it under VM's identity. For more info, refer https://aka.ms/RunCommandManagedLinux", download.GetUriForLogging(cfg.ScriptURI()))),
212212
constants.FileDownload_GenericError
213213
}
214214

@@ -217,7 +217,7 @@ func enable(ctx *log.Context, h types.HandlerEnvironment, report *types.RunComma
217217
errMessage := fmt.Sprintf("Failed to download artifacts: %v", err)
218218
extensionEvents.LogErrorEvent("enable", errMessage)
219219
return "", "",
220-
handlersettings.InternalWrapErrorWithClarification(err, "Artifact downloads failed. Use either a public artifact URI that points to .sh file, Azure storage blob SAS URI, or storage blob accessible by a managed identity and retry."),
220+
vmextension.CreateWrappedErrorWithClarification(err, "Artifact downloads failed. Use either a public artifact URI that points to .sh file, Azure storage blob SAS URI, or storage blob accessible by a managed identity and retry."),
221221
constants.ArtifactDownload_GenericError
222222
}
223223

@@ -236,7 +236,7 @@ func enable(ctx *log.Context, h types.HandlerEnvironment, report *types.RunComma
236236
if outputBlobAppendCreateOrReplaceError != nil {
237237
return "",
238238
"",
239-
handlersettings.InternalWrapErrorWithClarification(outputBlobAppendCreateOrReplaceError, fmt.Sprintf(blobCreateOrReplaceError, cfg.OutputBlobURI)),
239+
vmextension.CreateWrappedErrorWithClarification(outputBlobAppendCreateOrReplaceError, fmt.Sprintf(blobCreateOrReplaceError, cfg.OutputBlobURI)),
240240
constants.AppendBlobCreation_Other
241241
}
242242
}
@@ -254,7 +254,7 @@ func enable(ctx *log.Context, h types.HandlerEnvironment, report *types.RunComma
254254
if errorBlobAppendCreateOrReplaceError != nil {
255255
return "",
256256
"",
257-
handlersettings.InternalWrapErrorWithClarification(errorBlobAppendCreateOrReplaceError, fmt.Sprintf(blobCreateOrReplaceError, cfg.ErrorBlobURI)),
257+
vmextension.CreateWrappedErrorWithClarification(errorBlobAppendCreateOrReplaceError, fmt.Sprintf(blobCreateOrReplaceError, cfg.ErrorBlobURI)),
258258
constants.AppendBlobCreation_Other
259259
}
260260
}
@@ -564,7 +564,7 @@ func doRehydrateMrSeqFilesForProblematicUpgrades(ctx *log.Context, oldExtensionD
564564
}
565565

566566
// Copy files like *.mrseq (Most Recently executed Sequence number), .status files from old extension version to new extension version during update.
567-
func copyFiles(ctx log.Logger, fileExtensionSuffix string, extensionSubdirectory string, extensionEvents *extensionevents.ExtensionEventManager) (*list.List, error) {
567+
func copyFiles(ctx log.Logger, fileExtensionSuffix string, extensionSubdirectory string, extensionEvents *extensionevents.ExtensionEventManager) (*list.List, *vmextension.ErrorWithClarification) {
568568

569569
newExtensionVersion := os.Getenv(constants.ExtensionVersionEnvName)
570570
oldExtensionVersion := os.Getenv(constants.ExtensionVersionUpdatingFromEnvName)
@@ -588,15 +588,15 @@ func copyFiles(ctx log.Logger, fileExtensionSuffix string, extensionSubdirectory
588588
if errr != nil {
589589
errMessage := fmt.Sprintf("Failed to create directory '%s'", newExtensionDirectory)
590590
extensionEvents.LogErrorEvent("copyfiles", errMessage)
591-
return nil, vmextension.NewErrorWithClarification(constants.Internal_CouldNotCreateStatusDirectory, fmt.Errorf("Failed to create directory '%s': %v", newExtensionDirectory, err))
591+
return nil, vmextension.NewErrorWithClarificationPtr(constants.Internal_CouldNotCreateStatusDirectory, fmt.Errorf("Failed to create directory '%s': %v", newExtensionDirectory, err))
592592
}
593593
}
594594
}
595595

596596
if oldExtensionDirectory == "" || newExtensionDirectory == "" {
597597
errMessage := "oldExtesionDirectory or newExtensionDirectory is empty"
598598
extensionEvents.LogErrorEvent("copyfiles", errMessage)
599-
return nil, vmextension.NewErrorWithClarification(constants.Internal_ExtensionDirectoryNameEmpty, errors.New(errMessage))
599+
return nil, vmextension.NewErrorWithClarificationPtr(constants.Internal_ExtensionDirectoryNameEmpty, errors.New(errMessage))
600600
}
601601

602602
// Check if the directory exists
@@ -605,15 +605,15 @@ func copyFiles(ctx log.Logger, fileExtensionSuffix string, extensionSubdirectory
605605
errMessage := fmt.Sprintf("could not open sourceDirectory %s", oldExtensionDirectory)
606606
ctx.Log("message", errMessage)
607607
extensionEvents.LogErrorEvent("copyfiles", errMessage)
608-
return nil, vmextension.NewErrorWithClarification(constants.Internal_CouldNotOpenSubdirectory, fmt.Errorf("%s: %v", errMessage, err))
608+
return nil, vmextension.NewErrorWithClarificationPtr(constants.Internal_CouldNotOpenSubdirectory, fmt.Errorf("%s: %v", errMessage, err))
609609
}
610610

611611
directoryEntries, err := sourceDirectoryFDRef.ReadDir(0)
612612
if err != nil {
613613
errMessage := fmt.Sprintf("could not read directory entries from sourceDirectory %s", oldExtensionDirectory)
614614
ctx.Log("message", errMessage)
615615
extensionEvents.LogErrorEvent("copyfiles", errMessage)
616-
return nil, vmextension.NewErrorWithClarification(constants.Internal_CouldNotReadDirectoryEntries, fmt.Errorf("%s: %v", errMessage, err))
616+
return nil, vmextension.NewErrorWithClarificationPtr(constants.Internal_CouldNotReadDirectoryEntries, fmt.Errorf("%s: %v", errMessage, err))
617617
}
618618

619619
numberOfFilesMigrated := 0
@@ -631,7 +631,7 @@ func copyFiles(ctx log.Logger, fileExtensionSuffix string, extensionSubdirectory
631631
errMessage := "Failed to open '%s' file '%s' for reading. Contact ICM team AzureRT\\Extensions for this service error."
632632
ctx.Log("message", fmt.Sprintf(errMessage, fileExtensionSuffix, sourceFileFullPath))
633633
extensionEvents.LogErrorEvent("copyfiles", errMessage)
634-
return fileNamesMigrated, vmextension.NewErrorWithClarification(constants.Internal_FailedToOpenFileForReading, fmt.Errorf("%s: %v", errMessage, sourceFileOpenError))
634+
return fileNamesMigrated, vmextension.NewErrorWithClarificationPtr(constants.Internal_FailedToOpenFileForReading, fmt.Errorf("%s: %v", errMessage, sourceFileOpenError))
635635
}
636636
defer sourceFile.Close()
637637

@@ -640,7 +640,7 @@ func copyFiles(ctx log.Logger, fileExtensionSuffix string, extensionSubdirectory
640640
errMessage := "Failed to create '%s' file '%s'. Contact ICM team AzureRT\\Extensions for this service error."
641641
ctx.Log("message", fmt.Sprintf(errMessage, fileExtensionSuffix, destinationFileFullPath))
642642
extensionEvents.LogErrorEvent("copyfiles", errMessage)
643-
return fileNamesMigrated, vmextension.NewErrorWithClarification(constants.Internal_FailedToCreateFile, fmt.Errorf("%s: %v", errMessage, destFileCreateError))
643+
return fileNamesMigrated, vmextension.NewErrorWithClarificationPtr(constants.Internal_FailedToCreateFile, fmt.Errorf("%s: %v", errMessage, destFileCreateError))
644644
}
645645
defer destFile.Close()
646646

@@ -650,7 +650,7 @@ func copyFiles(ctx log.Logger, fileExtensionSuffix string, extensionSubdirectory
650650
fileExtensionSuffix, sourceFileFullPath, destinationFileFullPath)
651651
ctx.Log("message", errMessage)
652652
extensionEvents.LogErrorEvent("copyfiles", errMessage)
653-
return fileNamesMigrated, vmextension.NewErrorWithClarification(constants.Internal_FailedToCopyFile, fmt.Errorf("%s: %v", errMessage, copyError))
653+
return fileNamesMigrated, vmextension.NewErrorWithClarificationPtr(constants.Internal_FailedToCopyFile, fmt.Errorf("%s: %v", errMessage, copyError))
654654
} else {
655655
message := fmt.Sprintf("File '%s' was copied successfully to '%s'", sourceFileFullPath, destinationFileFullPath)
656656
ctx.Log("message", message)
@@ -783,12 +783,12 @@ func createDummyStatusFilesIfNeeded(ctx log.Logger, mrseqFilesNameList *list.Lis
783783

784784
// downloadScript downloads the script file specified in cfg into dir (creates if does
785785
// not exist) and takes storage credentials specified in cfg into account.
786-
func downloadScript(ctx *log.Context, dir string, cfg *handlersettings.HandlerSettings) (string, error) {
786+
func downloadScript(ctx *log.Context, dir string, cfg *handlersettings.HandlerSettings) (string, *vmextension.ErrorWithClarification) {
787787
// - prepare the output directory for files and the command output
788788
// - create the directory if missing
789789
ctx.Log("event", "creating output directory", "path", dir)
790790
if err := os.MkdirAll(dir, 0700); err != nil {
791-
return "", vmextension.NewErrorWithClarification(constants.FileDownload_CreateDirectoryFailure, err)
791+
return "", vmextension.NewErrorWithClarificationPtr(constants.FileDownload_CreateDirectoryFailure, err)
792792
}
793793
ctx.Log("event", "created output directory")
794794

@@ -812,7 +812,7 @@ func downloadScript(ctx *log.Context, dir string, cfg *handlersettings.HandlerSe
812812
return scriptFilePath, nil
813813
}
814814

815-
func downloadArtifacts(ctx *log.Context, dir string, cfg *handlersettings.HandlerSettings) error {
815+
func downloadArtifacts(ctx *log.Context, dir string, cfg *handlersettings.HandlerSettings) *vmextension.ErrorWithClarification {
816816
artifacts, err := cfg.ReadArtifacts()
817817
if err != nil {
818818
return err
@@ -828,7 +828,7 @@ func downloadArtifacts(ctx *log.Context, dir string, cfg *handlersettings.Handle
828828
filePath, err := files.DownloadAndProcessArtifact(ctx, dir, &artifacts[i])
829829
if err != nil {
830830
ctx.Log("events", "Failed to download artifact", err, "artifact", artifacts[i].ArtifactUri)
831-
return handlersettings.InternalWrapErrorWithClarification(err, "Failed to download artifact")
831+
return vmextension.CreateWrappedErrorWithClarification(err, "Failed to download artifact")
832832
}
833833

834834
ctx.Log("event", "Downloaded artifact complete", "file", filePath)
@@ -838,7 +838,7 @@ func downloadArtifacts(ctx *log.Context, dir string, cfg *handlersettings.Handle
838838
}
839839

840840
// runCmd runs the command (extracted from cfg) in the given dir (assumed to exist).
841-
func runCmd(ctx *log.Context, dir string, scriptFilePath string, cfg *handlersettings.HandlerSettings, metadata types.RCMetadata) (err error, exitCode int) {
841+
func runCmd(ctx *log.Context, dir string, scriptFilePath string, cfg *handlersettings.HandlerSettings, metadata types.RCMetadata) (err *vmextension.ErrorWithClarification, exitCode int) {
842842
ctx.Log("event", "executing command", "output", dir)
843843
var scenario string
844844

@@ -883,11 +883,11 @@ func runCmd(ctx *log.Context, dir string, scriptFilePath string, cfg *handlerset
883883
}
884884

885885
// base64 decode and optionally GZip decompress a script
886-
func decodeScript(script string) (string, string, error) {
886+
func decodeScript(script string) (string, string, *vmextension.ErrorWithClarification) {
887887
// scripts must be base64 encoded
888888
s, err := base64.StdEncoding.DecodeString(script)
889889
if err != nil {
890-
return "", "", vmextension.NewErrorWithClarification(constants.Script_FailedToDecode, errors.Wrap(err, "failed to decode script"))
890+
return "", "", vmextension.NewErrorWithClarificationPtr(constants.Script_FailedToDecode, errors.Wrap(err, "failed to decode script"))
891891
}
892892

893893
// scripts may be gzip'ed
@@ -901,14 +901,14 @@ func decodeScript(script string) (string, string, error) {
901901

902902
n, err := io.Copy(w, r)
903903
if err != nil {
904-
return "", "", vmextension.NewErrorWithClarification(constants.Script_FailedToDecompress, errors.Wrap(err, "failed to decompress script"))
904+
return "", "", vmextension.NewErrorWithClarificationPtr(constants.Script_FailedToDecompress, errors.Wrap(err, "failed to decompress script"))
905905
}
906906

907907
w.Flush()
908908
return buf.String(), fmt.Sprintf("%d;%d;gzip=1", len(script), n), nil
909909
}
910910

911-
func createOrReplaceAppendBlobUsingManagedIdentity(blobUri string, managedIdentity *handlersettings.RunCommandManagedIdentity) (*appendblob.Client, error) {
911+
func createOrReplaceAppendBlobUsingManagedIdentity(blobUri string, managedIdentity *handlersettings.RunCommandManagedIdentity) (*appendblob.Client, *vmextension.ErrorWithClarification) {
912912
var ID string = ""
913913
var miCred *azidentity.ManagedIdentityCredential = nil
914914
var miCredError error = nil
@@ -917,7 +917,7 @@ func createOrReplaceAppendBlobUsingManagedIdentity(blobUri string, managedIdenti
917917
if managedIdentity.ClientId != "" {
918918
ID = managedIdentity.ClientId
919919
} else if managedIdentity.ObjectId != "" { //ObjectId is not supported by azidentity.NewManagedIdentityCredential
920-
return nil, vmextension.NewErrorWithClarification(constants.AppendBlobCreation_ObjectIdNotSupported, errors.New("Managed identity's ObjectId is not supported. Use ClientId instead"))
920+
return nil, vmextension.NewErrorWithClarificationPtr(constants.AppendBlobCreation_ObjectIdNotSupported, errors.New("Managed identity's ObjectId is not supported. Use ClientId instead"))
921921
}
922922
}
923923

@@ -933,16 +933,16 @@ func createOrReplaceAppendBlobUsingManagedIdentity(blobUri string, managedIdenti
933933
if miCredError == nil {
934934
appendBlobClient, appendBlobNewClientError = appendblob.NewClient(blobUri, miCred, nil)
935935
if appendBlobNewClientError != nil {
936-
return nil, vmextension.NewErrorWithClarification(constants.AppendBlobCreation_ClientError, errors.Wrap(appendBlobNewClientError, fmt.Sprintf("Error Creating client to Append Blob '%s'. Make sure you are using Append blob. Other types of blob such as PageBlob, BlockBlob are not supported types.", download.GetUriForLogging(blobUri))))
936+
return nil, vmextension.NewErrorWithClarificationPtr(constants.AppendBlobCreation_ClientError, errors.Wrap(appendBlobNewClientError, fmt.Sprintf("Error Creating client to Append Blob '%s'. Make sure you are using Append blob. Other types of blob such as PageBlob, BlockBlob are not supported types.", download.GetUriForLogging(blobUri))))
937937
} else {
938938
// Create or Replace Append blob. If AppendBlob already exists, blob gets cleared.
939939
_, createAppendBlobError := appendBlobClient.Create(context.Background(), nil)
940940
if createAppendBlobError != nil {
941-
return nil, vmextension.NewErrorWithClarification(constants.AppendBlobCreation_Other, errors.Wrap(createAppendBlobError, fmt.Sprintf("Error creating or replacing the Append blob '%s'. Make sure you are using Append blob. Other types of blob such as PageBlob, BlockBlob are not supported types.", download.GetUriForLogging(blobUri))))
941+
return nil, vmextension.NewErrorWithClarificationPtr(constants.AppendBlobCreation_Other, errors.Wrap(createAppendBlobError, fmt.Sprintf("Error creating or replacing the Append blob '%s'. Make sure you are using Append blob. Other types of blob such as PageBlob, BlockBlob are not supported types.", download.GetUriForLogging(blobUri))))
942942
}
943943
}
944944
} else {
945-
return nil, vmextension.NewErrorWithClarification(constants.AppendBlobCreation_InvalidMsi, errors.Wrap(miCredError, "Error while retrieving managed identity credential"))
945+
return nil, vmextension.NewErrorWithClarificationPtr(constants.AppendBlobCreation_InvalidMsi, errors.Wrap(miCredError, "Error while retrieving managed identity credential"))
946946
}
947947

948948
return appendBlobClient, nil

internal/cmds/cmds_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/Azure/azure-extension-platform/pkg/extensionevents"
1616
"github.com/Azure/azure-extension-platform/pkg/handlerenv"
1717
"github.com/Azure/azure-extension-platform/pkg/logging"
18+
"github.com/Azure/azure-extension-platform/vmextension"
1819
"github.com/Azure/run-command-handler-linux/internal/constants"
1920
"github.com/Azure/run-command-handler-linux/internal/files"
2021
"github.com/Azure/run-command-handler-linux/internal/handlersettings"
@@ -377,7 +378,7 @@ func enable_extension(t *testing.T, fakeEnv types.HandlerEnvironment, tempDir st
377378
err = encoder.Encode(handlerSettings)
378379
require.Nil(t, err, "Could not serialze settings file")
379380

380-
RunCmd = func(ctx *log.Context, dir, scriptFilePath string, cfg *handlersettings.HandlerSettings, metadata types.RCMetadata) (error, int) {
381+
RunCmd = func(ctx *log.Context, dir, scriptFilePath string, cfg *handlersettings.HandlerSettings, metadata types.RCMetadata) (*vmextension.ErrorWithClarification, int) {
381382
wasCalled = true
382383
return nil, 0 // mock behavior
383384
}

internal/constants/errorclarification.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,7 @@ const (
132132
FileSystem_OpenStandardErrorFailed = 123
133133

134134
Immediate_Systemd_NotSupported = 140
135+
136+
Http_RequestFailure = 150
137+
Http_FailedStatusCode = 151
135138
)

0 commit comments

Comments
 (0)