44	"fmt" 
55	"strings" 
66
7+ 	"github.com/kubernetes-csi/csi-proxy/pkg/cim" 
78	"github.com/kubernetes-csi/csi-proxy/pkg/utils" 
89)
910
@@ -26,18 +27,22 @@ func New(requirePrivacy bool) *SmbAPI {
2627	}
2728}
2829
30+ func  remotePathForQuery (remotePath  string ) string  {
31+ 	return  strings .ReplaceAll (remotePath , "\\ " , "\\ \\ " )
32+ }
33+ 
2934func  (* 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 ))
3336	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 )
3538	}
3639
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 
3943	}
40- 	return  true , nil 
44+ 
45+ 	return  status .(int32 ) ==  cim .SmbMappingStatusOK , nil 
4146}
4247
4348// NewSmbLink - creates a directory symbolic link to the remote share. 
@@ -48,7 +53,6 @@ func (*SmbAPI) IsSmbMapped(remotePath string) (bool, error) {
4853// alpha to merge the paths. 
4954// TODO (for beta release): Merge the link paths - os.Symlink and Powershell link path. 
5055func  (* SmbAPI ) NewSmbLink (remotePath , localPath  string ) error  {
51- 
5256	if  ! strings .HasSuffix (remotePath , "\\ " ) {
5357		// Golang has issues resolving paths mapped to file shares if they do not end in a trailing \ 
5458		// so add one if needed. 
@@ -78,12 +82,26 @@ func (api *SmbAPI) NewSmbGlobalMapping(remotePath, username, password string) er
7882		return  fmt .Errorf ("NewSmbGlobalMapping failed. output: %q, err: %v" , string (output ), err )
7983	}
8084	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 
8198}
8299
83100func  (* 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 )
87104	}
105+ 
88106	return  nil 
89107}
0 commit comments