-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Completed Source_PhysicalServer workflow #4
Open
ggCohesity
wants to merge
2
commits into
v1.1
Choose a base branch
from
physicalServer
base: v1.1
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package cohesity | ||
|
||
import ( | ||
"errors" | ||
"log" | ||
"strconv" | ||
|
||
CohesityManagementSdk "github.com/cohesity/management-sdk-go/managementsdk" | ||
"github.com/cohesity/management-sdk-go/models" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
//ProtectionSourceID Id of protection source registered. | ||
var ProtectionSourceID int64 | ||
|
||
func resourceCohesitySourcePhysicalServer() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceCohesitySourcePhysicalServerCreate, | ||
Read: resourceCohesitySourcePhysicalServerRead, | ||
Delete: resourceCohesitySourcePhysicalServerDelete, | ||
Update: resourceCohesitySourcePhysicalServerUpdate, | ||
Schema: map[string]*schema.Schema{ | ||
"endpoint": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: `Specifies the network endpoint of the Protection Source where it is reachable. It could be an URL or hostname or | ||
an IP address of the Protection Source`, | ||
}, | ||
"force_register": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: true, | ||
Description: "Forcefully register physical server to target cluster", | ||
}, | ||
"host_type": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "OS type on the physical server.", | ||
}, | ||
"physical_type": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "Host Type for physical server.", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceCohesitySourcePhysicalServerCreate(resourceData *schema.ResourceData, configMetaData interface{}) error { | ||
log.Printf("[INFO] Starting Physical Server Registration") | ||
var cohesityConfig = configMetaData.(Config) | ||
client, err := CohesityManagementSdk.NewCohesitySdkClient(cohesityConfig.clusterVip, | ||
cohesityConfig.clusterUsername, cohesityConfig.clusterPassword, cohesityConfig.clusterDomain) | ||
if err != nil { | ||
log.Printf(err.Error()) | ||
return errors.New("Failed to authenticate with Cohesity") | ||
} | ||
var endpoint = resourceData.Get("endpoint").(string) | ||
var forceRegister = resourceData.Get("force_register").(bool) | ||
|
||
//Creating Request Body | ||
var requestBody models.RegisterProtectionSourceParameters | ||
requestBody.Endpoint = &endpoint | ||
requestBody.ForceRegister = &forceRegister | ||
requestBody.Environment = models.EnvironmentRegisterProtectionSourceParameters_KPHYSICAL | ||
requestBody.HostType = models.HostTypeRegisterProtectionSourceParameters_KLINUX | ||
requestBody.PhysicalType = models.PhysicalType_KHOST | ||
|
||
log.Printf("[INFO] Register Physical Server as protection source %s", endpoint) | ||
result, err := client.ProtectionSources().CreateRegisterProtectionSource(&requestBody) | ||
|
||
if err != nil { | ||
log.Printf(err.Error()) | ||
return errors.New("Failed to register source Physical Server") | ||
} | ||
resourceData.SetId(strconv.FormatInt(*result.Id, 10)) | ||
log.Printf("[INFO] Successfully registered physical server protection source %s", endpoint) | ||
log.Printf("[INFO] ID %s", resourceData.Id()) | ||
return resourceCohesitySourcePhysicalServerRead(resourceData, configMetaData) | ||
} | ||
|
||
func resourceCohesitySourcePhysicalServerRead(resourceData *schema.ResourceData, configMetaData interface{}) error { | ||
log.Printf("[INFO] Starting Read") | ||
var cohesityConfig = configMetaData.(Config) | ||
client, err := CohesityManagementSdk.NewCohesitySdkClient(cohesityConfig.clusterVip, | ||
cohesityConfig.clusterUsername, cohesityConfig.clusterPassword, cohesityConfig.clusterDomain) | ||
if err != nil { | ||
log.Printf(err.Error()) | ||
return errors.New("Failed to authenticate with Cohesity") | ||
} | ||
var trueValue = true | ||
var environmentType = []models.EnvironmentListProtectionSourcesEnum{models.EnvironmentListProtectionSources_KPHYSICAL} | ||
var endpoint = resourceData.Get("endpoint").(string) | ||
|
||
// Converting to int64 | ||
ProtectionSourceID, _ = strconv.ParseInt(resourceData.Id(), 10, 64) | ||
log.Printf("[INFO] Read Cohesity Physical Server protection source %v, %T", ProtectionSourceID, ProtectionSourceID) | ||
|
||
result, err := client.ProtectionSources().ListProtectionSources(&ProtectionSourceID, nil, nil, &trueValue, &trueValue, &trueValue, environmentType, nil, &trueValue, nil, nil, &trueValue) | ||
|
||
for _, protectionSource := range result { | ||
log.Printf("[INFO] Protection Name: %s", *protectionSource.ProtectionSource.Name) | ||
if *protectionSource.ProtectionSource.Name == endpoint { | ||
log.Printf("[INFO] Found the Physical Server protection source %s on cohesity cluster", | ||
*protectionSource.ProtectionSource.Name) | ||
return nil | ||
} | ||
} | ||
log.Printf("[INFO] Couldn't find the Physical Server protection source %s", err) | ||
resourceData.SetId("") | ||
return nil | ||
} | ||
|
||
func resourceCohesitySourcePhysicalServerDelete(resourceData *schema.ResourceData, configMetaData interface{}) error { | ||
log.Printf("[INFO] Starting Delete") | ||
var cohesityConfig = configMetaData.(Config) | ||
client, err := CohesityManagementSdk.NewCohesitySdkClient(cohesityConfig.clusterVip, | ||
cohesityConfig.clusterUsername, cohesityConfig.clusterPassword, cohesityConfig.clusterDomain) | ||
if err != nil { | ||
log.Printf(err.Error()) | ||
return errors.New("Failed to authenticate with Cohesity") | ||
} | ||
|
||
// Converting type to int64 | ||
protectionSourceID, _ := strconv.ParseInt(resourceData.Id(), 10, 64) | ||
log.Printf("[INFO] Unregistering the Physical Server protection source %s", resourceData. | ||
Get("endpoint").(string)) | ||
|
||
err = client.ProtectionSources().DeleteUnregisterProtectionSource(protectionSourceID) | ||
if err != nil { | ||
log.Printf(err.Error()) | ||
return errors.New("Failed to unregister Physical Server protection source") | ||
} | ||
log.Printf("[INFO] Successfully unregistered the Physical Server protection source %s", | ||
resourceData.Get("endpoint").(string)) | ||
return nil | ||
} | ||
|
||
func resourceCohesitySourcePhysicalServerUpdate(resourceData *schema.ResourceData, configMetaData interface{}) error { | ||
return errors.New("Update Operation is unavailable for source Physical Server") | ||
} |
131 changes: 131 additions & 0 deletions
131
cohesity/resource_cohesity_source_physicalServer_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package cohesity | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log" | ||
"testing" | ||
|
||
CohesityManagementSdk "github.com/cohesity/management-sdk-go/managementsdk" | ||
"github.com/cohesity/management-sdk-go/models" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccSourcePhysicalServer(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckSourcePhysicalServerDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccSourcePhysicalServerConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckSourcePhysicalServerCreated(), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckSourcePhysicalServerCreated() resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
var cohesityConfig = testAccProvider.Meta().(Config) | ||
client, err := CohesityManagementSdk.NewCohesitySdkClient(cohesityConfig.clusterVip, | ||
cohesityConfig.clusterUsername, cohesityConfig.clusterPassword, cohesityConfig.clusterDomain) | ||
if err != nil { | ||
log.Printf(err.Error()) | ||
return errors.New("Failed to authenticate with Cohesity") | ||
} | ||
|
||
var endpoint = "10.2.150.118" | ||
var environmentType = []models.EnvironmentListProtectionSourcesEnum{models.EnvironmentListProtectionSources_KPHYSICAL} | ||
var trueValue = true | ||
log.Printf("[INFO] Get list of Physical Server sources to verify %s creation", endpoint) | ||
|
||
log.Println("[INFO] Access to Global", ProtectionSourceID) | ||
// result, err := client.ProtectionSources(). | ||
// ListProtectionSources(nil, nil, nil, &trueValue, &trueValue, | ||
// &trueValue, environmentType, nil, nil, nil, nil, nil) | ||
|
||
result, err := client.ProtectionSources().ListProtectionSources(&ProtectionSourceID, nil, nil, &trueValue, &trueValue, &trueValue, environmentType, nil, &trueValue, nil, nil, &trueValue) | ||
|
||
if err != nil { | ||
log.Printf(err.Error()) | ||
return fmt.Errorf("Failed to get Physical Server protection sources") | ||
} | ||
|
||
for _, protectionSource := range result { | ||
if *protectionSource.ProtectionSource.Name == endpoint { | ||
log.Printf("[INFO] Found the Physical Server protection source %s on cohesity cluster", endpoint) | ||
//validate the Physical Server Source Registration | ||
if *protectionSource.LogicalSize == 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are you checking the logical size here, we can just check the name, if its there then return nil |
||
return fmt.Errorf("Failed to valaidate created physical server source") | ||
} | ||
// Check if registered | ||
// Check if deleted | ||
|
||
// *protectionSource.RegistrationInfo.AccessInfo.Endpoint != endpoint { | ||
// return fmt.Errorf("Failed to valaidate created physical server source") | ||
// } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you pls remove all the commented code |
||
|
||
// if *protectionSource.RegistrationInfo.ThrottlingPolicy.IsEnabled != true || | ||
// *protectionSource.RegistrationInfo.ThrottlingPolicy.EnforceMaxStreams != true || | ||
// *protectionSource.RegistrationInfo.ThrottlingPolicy.MaxConcurrentStreams != 5 || | ||
// *protectionSource.RegistrationInfo.ThrottlingPolicy.LatencyThresholds.NewTaskMsecs != 110 || | ||
// *protectionSource.RegistrationInfo.ThrottlingPolicy.LatencyThresholds.ActiveTaskMsecs != 120 { | ||
// return fmt.Errorf("Failed to valaidate throttling policy parameters of created Vmware source") | ||
// } | ||
return nil | ||
} | ||
} | ||
return fmt.Errorf("Failed to create Physical Server protection source: %s", endpoint) | ||
} | ||
} | ||
|
||
func testAccCheckSourcePhysicalServerDestroy(s *terraform.State) error { | ||
var cohesityConfig = testAccProvider.Meta().(Config) | ||
client, err := CohesityManagementSdk.NewCohesitySdkClient(cohesityConfig.clusterVip, | ||
cohesityConfig.clusterUsername, cohesityConfig.clusterPassword, cohesityConfig.clusterDomain) | ||
if err != nil { | ||
log.Printf(err.Error()) | ||
return fmt.Errorf("Failed to authenticate with Cohesity") | ||
} | ||
|
||
var endpoint = "10.2.150.118" | ||
var environmentType = []models.EnvironmentListProtectionSourcesEnum{models.EnvironmentListProtectionSources_KPHYSICAL} | ||
var trueValue = true | ||
log.Printf("[INFO] Get list of Physical Server protection sources to verify %s deletion", endpoint) | ||
|
||
result, err := client.ProtectionSources(). | ||
ListProtectionSources(nil, nil, nil, &trueValue, &trueValue, | ||
&trueValue, environmentType, nil, nil, nil, nil, nil) | ||
if err != nil { | ||
log.Printf(err.Error()) | ||
return fmt.Errorf("Failed to get Physical Server protection sources") | ||
} | ||
|
||
for _, protectionSource := range result { | ||
log.Printf("[INFO] Protection Name: %s", *protectionSource.ProtectionSource.Name) | ||
if *protectionSource.ProtectionSource.Name == endpoint { | ||
log.Printf("[INFO] Found the Physical Server protection source %s on cohesity cluster", | ||
*protectionSource.ProtectionSource.Name) | ||
return fmt.Errorf("Failed to unregister the Physical Server protection source %s", endpoint) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
const testAccSourcePhysicalServerConfig = ` | ||
provider "cohesity" { | ||
cluster_vip = "10.2.145.47" | ||
cluster_username = "admin" | ||
cluster_domain = "LOCAL" | ||
} | ||
|
||
resource "cohesity_source_physical_server" "physical_server"{ | ||
endpoint = "10.2.150.118" | ||
force_register = true | ||
host_type = "kWindows" | ||
physical_type = "kHost" | ||
} | ||
` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add a comment here to tell what strconv.ParseInt(resourceData.Id(), 10, 64) this is doing