4
4
"fmt"
5
5
"strings"
6
6
7
+ "github.com/kubernetes-csi/csi-proxy/pkg/cim"
7
8
"github.com/kubernetes-csi/csi-proxy/pkg/utils"
8
9
)
9
10
@@ -26,18 +27,22 @@ func New(requirePrivacy bool) *SmbAPI {
26
27
}
27
28
}
28
29
30
+ func remotePathForQuery (remotePath string ) string {
31
+ return strings .ReplaceAll (remotePath , "\\ " , "\\ \\ " )
32
+ }
33
+
29
34
func (* SmbAPI ) IsSmbMapped (remotePath string ) (bool , error ) {
30
- cmdLine := `$(Get-SmbGlobalMapping -RemotePath $Env:smbremotepath -ErrorAction Stop).Status `
31
- cmdEnv := fmt .Sprintf ("smbremotepath=%s" , remotePath )
32
- out , err := utils .RunPowershellCmd (cmdLine , cmdEnv )
35
+ inst , err := cim .QuerySmbGlobalMappingByRemotePath (remotePathForQuery (remotePath ))
33
36
if err != nil {
34
- return false , fmt . Errorf ( "error checking smb mapping. cmd %s, output: %s, err: %v" , remotePath , string ( out ), err )
37
+ return false , cim . IgnoreNotFound ( err )
35
38
}
36
39
37
- if len (out ) == 0 || ! strings .EqualFold (strings .TrimSpace (string (out )), "OK" ) {
38
- return false , nil
40
+ status , err := inst .GetProperty ("Status" )
41
+ if err != nil {
42
+ return false , err
39
43
}
40
- return true , nil
44
+
45
+ return status .(int32 ) == cim .SmbMappingStatusOK , nil
41
46
}
42
47
43
48
// NewSmbLink - creates a directory symbolic link to the remote share.
@@ -48,7 +53,6 @@ func (*SmbAPI) IsSmbMapped(remotePath string) (bool, error) {
48
53
// alpha to merge the paths.
49
54
// TODO (for beta release): Merge the link paths - os.Symlink and Powershell link path.
50
55
func (* SmbAPI ) NewSmbLink (remotePath , localPath string ) error {
51
-
52
56
if ! strings .HasSuffix (remotePath , "\\ " ) {
53
57
// Golang has issues resolving paths mapped to file shares if they do not end in a trailing \
54
58
// so add one if needed.
@@ -78,12 +82,26 @@ func (api *SmbAPI) NewSmbGlobalMapping(remotePath, username, password string) er
78
82
return fmt .Errorf ("NewSmbGlobalMapping failed. output: %q, err: %v" , string (output ), err )
79
83
}
80
84
return nil
85
+ //TODO: move to use WMI when the credentials could be correctly handled
86
+ //params := map[string]interface{}{
87
+ // "RemotePath": remotePath,
88
+ // "RequirePrivacy": api.RequirePrivacy,
89
+ //}
90
+ //if username != "" {
91
+ // params["Credential"] = fmt.Sprintf("%s:%s", username, password)
92
+ //}
93
+ //result, _, err := cim.InvokeCimMethod(cim.WMINamespaceSmb, "MSFT_SmbGlobalMapping", "Create", params)
94
+ //if err != nil {
95
+ // return fmt.Errorf("NewSmbGlobalMapping failed. result: %d, err: %v", result, err)
96
+ //}
97
+ //return nil
81
98
}
82
99
83
100
func (* SmbAPI ) RemoveSmbGlobalMapping (remotePath string ) error {
84
- cmd := `Remove-SmbGlobalMapping -RemotePath $Env:smbremotepath -Force`
85
- if output , err := utils . RunPowershellCmd ( cmd , fmt . Sprintf ( "smbremotepath=%s" , remotePath )); err != nil {
86
- return fmt .Errorf ("UnmountSmbShare failed. output: %q, err: %v" , string ( output ) , err )
101
+ err := cim . RemoveSmbGlobalMappingByRemotePath ( remotePathForQuery ( remotePath ))
102
+ if err != nil {
103
+ return fmt .Errorf ("error remove smb mapping '%s'. err: %v" , remotePath , err )
87
104
}
105
+
88
106
return nil
89
107
}
0 commit comments