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,17 +27,16 @@ 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
+ _ , 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 , err
35
38
}
36
39
37
- if len (out ) == 0 || ! strings .EqualFold (strings .TrimSpace (string (out )), "OK" ) {
38
- return false , nil
39
- }
40
40
return true , nil
41
41
}
42
42
@@ -48,7 +48,6 @@ func (*SmbAPI) IsSmbMapped(remotePath string) (bool, error) {
48
48
// alpha to merge the paths.
49
49
// TODO (for beta release): Merge the link paths - os.Symlink and Powershell link path.
50
50
func (* SmbAPI ) NewSmbLink (remotePath , localPath string ) error {
51
-
52
51
if ! strings .HasSuffix (remotePath , "\\ " ) {
53
52
// Golang has issues resolving paths mapped to file shares if they do not end in a trailing \
54
53
// so add one if needed.
@@ -78,12 +77,26 @@ func (api *SmbAPI) NewSmbGlobalMapping(remotePath, username, password string) er
78
77
return fmt .Errorf ("NewSmbGlobalMapping failed. output: %q, err: %v" , string (output ), err )
79
78
}
80
79
return nil
80
+ //TODO: move to use WMI when the credentials could be correctly handled
81
+ //params := map[string]interface{}{
82
+ // "RemotePath": remotePath,
83
+ // "RequirePrivacy": api.RequirePrivacy,
84
+ //}
85
+ //if username != "" {
86
+ // params["Credential"] = fmt.Sprintf("%s:%s", username, password)
87
+ //}
88
+ //result, _, err := cim.InvokeCimMethod(cim.WMINamespaceSmb, "MSFT_SmbGlobalMapping", "Create", params)
89
+ //if err != nil {
90
+ // return fmt.Errorf("NewSmbGlobalMapping failed. result: %d, err: %v", result, err)
91
+ //}
92
+ //return nil
81
93
}
82
94
83
95
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 )
96
+ err := cim . RemoveSmbGlobalMappingByRemotePath ( remotePathForQuery ( remotePath ))
97
+ if err != nil {
98
+ return fmt .Errorf ("error remove smb mapping '%s'. err: %v" , remotePath , err )
87
99
}
100
+
88
101
return nil
89
102
}
0 commit comments