From 64284a28651ebe5f507775a0a797ac76a361d53e Mon Sep 17 00:00:00 2001 From: zengyingzhe <52645009+zengyingzhe@users.noreply.github.com> Date: Sat, 11 Jan 2020 17:23:52 +0800 Subject: [PATCH] enhance the volume detachment process (#18) --- contrib/drivers/huawei/oceanstor/client.go | 63 ++++++++++++++++--- contrib/drivers/huawei/oceanstor/constants.go | 6 ++ contrib/drivers/huawei/oceanstor/oceanstor.go | 17 +++-- 3 files changed, 71 insertions(+), 15 deletions(-) diff --git a/contrib/drivers/huawei/oceanstor/client.go b/contrib/drivers/huawei/oceanstor/client.go index 60344ea24..68403fb61 100644 --- a/contrib/drivers/huawei/oceanstor/client.go +++ b/contrib/drivers/huawei/oceanstor/client.go @@ -1021,9 +1021,14 @@ func (c *OceanStorClient) GetHostLunId(hostId, lunId string) (int, error) { func (c *OceanStorClient) RemoveLunFromLunGroup(lunGrpId, lunId string) error { url := fmt.Sprintf("/lungroup/associate?ID=%s&ASSOCIATEOBJTYPE=11&ASSOCIATEOBJID=%s", lunGrpId, lunId) if err := c.request("DELETE", url, nil, nil); err != nil { + if c.checkErrorCode(err, ErrorObjectUnavailable) { + return nil + } + log.Errorf("Remove lun %s from lun group %s failed, %v", lunId, lunGrpId, err) return err } + log.Infof("Remove lun %s from lun group %s success", lunId, lunGrpId) return nil } @@ -1033,16 +1038,23 @@ func (c *OceanStorClient) RemoveLunGroupFromMappingView(viewId, lunGrpId string) log.Infof("Lun group %s has already been removed from mapping view %s", lunGrpId, viewId) return nil } + url := "/mappingview/REMOVE_ASSOCIATE" data := map[string]interface{}{ "ASSOCIATEOBJTYPE": ObjectTypeLunGroup, "ASSOCIATEOBJID": lunGrpId, "TYPE": ObjectTypeMappingView, "ID": viewId} + if err := c.request("PUT", url, data, nil); err != nil { + if c.checkErrorCode(err, ErrorLunGroupNotInMappingView) { + return nil + } + log.Errorf("Remove lun group %s from mapping view %s failed", lunGrpId, viewId) return err } + log.Infof("Remove lun group %s from mapping view %s success", lunGrpId, viewId) return nil } @@ -1052,58 +1064,93 @@ func (c *OceanStorClient) RemoveHostGroupFromMappingView(viewId, hostGrpId strin log.Infof("Host group %s has already been removed from mapping view %s", hostGrpId, viewId) return nil } + url := "/mappingview/REMOVE_ASSOCIATE" data := map[string]interface{}{ "ASSOCIATEOBJTYPE": ObjectTypeHostGroup, "ASSOCIATEOBJID": hostGrpId, "TYPE": ObjectTypeMappingView, "ID": viewId} + if err := c.request("PUT", url, data, nil); err != nil { + if c.checkErrorCode(err, ErrorHostGroupNotInMappingView) { + return nil + } + log.Errorf("Remove host group %s from mapping view %s failed", hostGrpId, viewId) return err } + log.Infof("Remove host group %s from mapping view %s success", hostGrpId, viewId) return nil } func (c *OceanStorClient) RemoveHostFromHostGroup(hostGrpId, hostId string) error { - url := fmt.Sprintf("/host/associate?TYPE=14&ID=%s&ASSOCIATEOBJTYPE=21&ASSOCIATEOBJID=%s", hostGrpId, hostId) if err := c.request("DELETE", url, nil, nil); err != nil { + if c.checkErrorCode(err, ErrorHostNotInHostGroup) { + return nil + } + log.Errorf("Remove host %s from host group %s failed", hostId, hostGrpId) return err } + log.Infof("Remove host %s from host group %s success", hostId, hostGrpId) return nil } func (c *OceanStorClient) RemoveIscsiFromHost(initiator string) error { - url := "/iscsi_initiator/remove_iscsi_from_host" data := map[string]interface{}{"TYPE": ObjectTypeIscsiInitiator, "ID": initiator} if err := c.request("PUT", url, data, nil); err != nil { + if c.checkErrorCode(err, ErrorInitiatorNotInHost) { + return nil + } + log.Errorf("Remove initiator %s failed", initiator) return err } + log.Infof("Remove initiator %s success", initiator) return nil } func (c *OceanStorClient) DeleteHostGroup(id string) error { - return c.request("DELETE", "/hostgroup/"+id, nil, nil) + err := c.request("DELETE", "/hostgroup/"+id, nil, nil) + if err != nil && c.checkErrorCode(err, ErrorHostGroupNotExist) { + return nil + } + + return err } func (c *OceanStorClient) DeleteLunGroup(id string) error { - return c.request("DELETE", "/LUNGroup/"+id, nil, nil) + err := c.request("DELETE", "/LUNGroup/"+id, nil, nil) + if err != nil && c.checkErrorCode(err, ErrorObjectUnavailable) { + return nil + } + + return err } func (c *OceanStorClient) DeleteHost(id string) error { - return c.request("DELETE", "/host/"+id, nil, nil) + err := c.request("DELETE", "/host/"+id, nil, nil) + if err != nil && c.checkErrorCode(err, ErrorHostNotExist) { + return nil + } + + return err } func (c *OceanStorClient) DeleteMappingView(id string) error { - return c.request("DELETE", "/mappingview/"+id, nil, nil) + err := c.request("DELETE", "/mappingview/"+id, nil, nil) + if err != nil && c.checkErrorCode(err, ErrorMappingViewNotExist) { + return nil + } + + return err } func (c *OceanStorClient) GetArrayInfo() (*System, error) { @@ -1303,10 +1350,6 @@ func (c *OceanStorClient) IsHostAssociatedToHostgroup(hostId string) (bool, erro return false, nil } -func (c *OceanStorClient) RemoveHost(hostId string) error { - return c.request("DELETE", fmt.Sprintf("/host/%s", hostId), nil, nil) -} - func (c *OceanStorClient) AddFCPortTohost(hostId string, wwn string) error { url := fmt.Sprintf("/fc_initiator/%s", wwn) data := map[string]interface{}{ diff --git a/contrib/drivers/huawei/oceanstor/constants.go b/contrib/drivers/huawei/oceanstor/constants.go index c07b0a7de..6303d37a4 100644 --- a/contrib/drivers/huawei/oceanstor/constants.go +++ b/contrib/drivers/huawei/oceanstor/constants.go @@ -63,6 +63,12 @@ const ( ErrorHostGroupAlreadyInMappingView = 1073804556 ErrorLunGroupAlreadyInMappingView = 1073804560 ErrorLunNotExist = 1077936859 + ErrorLunGroupNotInMappingView = 1073804554 + ErrorHostGroupNotInMappingView = 1073804552 + ErrorHostNotInHostGroup = 1073745412 + ErrorHostNotExist = 1077937498 + ErrorMappingViewNotExist = 1077951819 + ErrorInitiatorNotInHost = 1077950342 ) // misc diff --git a/contrib/drivers/huawei/oceanstor/oceanstor.go b/contrib/drivers/huawei/oceanstor/oceanstor.go index 5530342c5..9b69b2d95 100644 --- a/contrib/drivers/huawei/oceanstor/oceanstor.go +++ b/contrib/drivers/huawei/oceanstor/oceanstor.go @@ -627,7 +627,7 @@ func (d *Driver) connectFCUseNoSwitch(opt *pb.CreateVolumeAttachmentOpts, initia } if wwnsInHost == nil && iqnsInHost == nil && flag == false { - if err = d.client.RemoveHost(hostId); err != nil { + if err = d.client.DeleteHost(hostId); err != nil { return nil, nil, err } } @@ -746,19 +746,26 @@ func (d *Driver) deleteZoneAndRemoveFCInitiators(wwns []string, hostId, hostGrpI func (d *Driver) getMappedInfo(hostName string) (string, string, string, string, error) { hostId, err := d.client.GetHostIdByName(hostName) if err != nil { + if IsNotFoundError(err) { + log.Warningf("host(%s) has been removed already, ignore it.", hostName) + return "", "", "", "", nil + } + return "", "", "", "", err } lunGrpId, err := d.client.FindLunGroup(PrefixLunGroup + hostId) - if err != nil { + if err != nil && !IsNotFoundError(err) { return "", "", "", "", err } + hostGrpId, err := d.client.FindHostGroup(PrefixHostGroup + hostId) - if err != nil { + if err != nil && !IsNotFoundError(err) { return "", "", "", "", err } + viewId, err := d.client.FindMappingView(PrefixMappingView + hostId) - if err != nil { + if err != nil && !IsNotFoundError(err) { return "", "", "", "", err } @@ -806,7 +813,7 @@ func (d *Driver) clearHostRelatedResource(lunGrpId, viewId, hostId, hostGrpId st return err } if !flag { - if err := d.client.RemoveHost(hostId); err != nil { + if err := d.client.DeleteHost(hostId); err != nil { return err } }